gpt4 book ai didi

ruby-on-rails - Rails Rspec 测试 html 选择选项

转载 作者:行者123 更新时间:2023-11-28 21:13:27 26 4
gpt4 key购买 nike

我正在尝试使用 rspec 来测试从 select 标记中选择一个值。选择标记应列出数据库中每个用户的所有名称。当我浏览该网站时,它会起作用。但是测试好像没有看到FactoryGirl创建的用户名:

错误是:

cannot select option, no option with text 'Person 4' in select box 'receiver_id'

这是 message_pages 规范的一部分:

let(:user) { FactoryGirl.create(:user) }
let(:other_user) { FactoryGirl.create(:user) }
before do
select other_user.name, :from => 'message[receiver_id]'
fill_in "Subject", with: "Hello"
fill_in "Body", with: "It's time to have fun!"
end

这是我的 View 的 HTML 输出:

<label for="message_receiver_id">Receiver</label>
<select id="message_receiver_id" name="message[receiver_id]"><optionvalue="12">John</option>
<option value="13">Tom</option>
<option value="9">Jay</option>
<option value="14">Bob</option></select>

最佳答案

这看起来像是一个惰性评估问题,在使用 let 时很常见。来自documentation :

Note that let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked. You can use let! to force the method’s invocation before each example.

因此 userother_user 只会在您第一次调用它们时创建,如果您已经渲染了页面,那就太晚了。尝试将 let 调用切换为 let!,看看是否能解决问题。

(有趣的是,您说切换到 user.name 有效。您能发布规范中的其他初始化代码吗?)

关于ruby-on-rails - Rails Rspec 测试 html 选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12966945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com