【发布时间】:2015-09-28 16:36:01
【问题描述】:
我的User 模型上有一个time_zone 属性,用于跟踪用户的首选时区。默认以nil 开头。
在他们的个人编辑页面上,用户可以通过从下拉列表中选择另一个时区来更改他们的时区。
我的测试如下——
it "updates the user's time zone on submit" do
# Creates with FactoryGirl
@user = create(:user)
# BEFORE state confirmation
expect(@user.time_zone).to be_nil
# Simulate the user selecting a timezone from the dropdown on their edit page.
# Capybara is used for selection and clicking.
visit edit_profile_path
select_time_zone_from_dropdown("America/New_York")
# Submit, which sends a POST request to update the User model
click_button("Submit")
# AFTER state confirmation - this FAILS
# Apparently the reload doesn't work and `time_zone` is still set to
# nil for this user
@user.reload
expect(@user.time_zone).to eq("America/New_York")
end
如您所见,测试失败是因为 time_zone 从未为此用户记录更新。
这就是奇怪的地方 -
在数据库中,正确的值确实发生了变化。只是
ActiveRecord对象没有为了绕过#1,我尝试重新加载对象属性,但没有成功
如果我在
reload之后使用binding.pry暂停它并运行@user.reload手动,它可以工作。所以它只是在测试套件期间运行时不起作用。
缓存和重新加载值的方式有什么奇怪的吗?
谢谢!
【问题讨论】:
-
您确定@user 对象是被更改的对象吗? “edit_profile_path”如何知道要编辑哪个用户?
-
可能是时间问题,你能不能加一个
expect(page).to have('some_selector')然后@user.reload
标签: ruby-on-rails ruby activerecord rspec