Laravel9+Vue3+Inertia环境下如何测试字符串是否存在?
在Laravel 9 + Vue 3 + Inertia环境中测试响应是否包含指定字符串
你说得对,has方法确实是用来验证Inertia传递到前端的页面属性/数据变量是否存在,并非用于检测页面渲染后的文本内容。针对你的需求,有两种可行的解决方案:
方案一:验证Inertia传递的Props值
如果"all rights reserved"是通过Inertia的props传递到Vue组件中渲染的,直接验证对应的prop值即可:
public function test_footer_is_loading() { $user = User::factory()->create(); $this ->actingAs($user) ->get(route('home')) ->assertInertia(fn (Assert $assert) => $assert // 假设版权文本通过名为copyright的prop传递,根据实际情况修改键名 ->where('copyright', 'all rights reserved') ); }
方案二:直接检测响应的HTML内容
如果文本是直接写在Vue组件模板里(未通过props传递),可以使用Laravel的assertSee方法断言响应HTML中包含指定字符串:
public function test_footer_is_loading() { $user = User::factory()->create(); $this ->actingAs($user) ->get(route('home')) ->assertSee('all rights reserved'); }
注意:若你的项目采用纯客户端渲染(未启用SSR),
assertSee可能无法检测到Vue动态渲染的内容。这种情况下更推荐使用方案一,或者选择Cypress等端到端测试工具来验证页面实际渲染效果。
内容的提问来源于stack exchange,提问作者im64bit
相关产品推荐
相关产品推荐

