Rails 3 / Capybara 不保存模型属性
我正在尝试在我的 Rails 3 应用程序上运行 Capybara 1.0 测试,以测试当用户单击确认链接时,他是否真正得到确认。
现在,当我手动测试时,这实际上是有效的。另外,正如你所看到的,我在confirm方法中放入了一行puts @user.confirmed来调试它,当我运行测试时它实际上打印了true。然而,测试本身失败了。
执行控制器方法后,测试似乎没有记住我的用户模型中已确认的属性。
我缺少什么?预先非常感谢。
测试:
it "should allow a user to be confirmed after clicking confirmation link" do
fill_in('user_email', :with => '[email protected]')
click_button('Submit')
@user = User.find_by_email('[email protected]')
@user.confirmed.should be_false
visit confirm_path(@user.confirmation_code)
@user.confirmed.should be_true
end
控制器方法:
def confirm
@confirmation_code = params[:confirmation_code]
@user = User.find_by_confirmation_code(@confirmation_code)
@website = @user.website
@user.confirm
if @user.referrer_id
User.find(@user.referrer_id).increment_signups
end
flash[:success] = "Thanks for signing up!"
flash[:user_show] = @user.id
puts @user.confirmed
redirect_to "http://" + @website.domain_name
end
用户模型方法:
def confirm
self.confirmed = true
self.save
end
I'm trying to run a Capybara 1.0 test on my Rails 3 app to test whether when a user clicks on a confirmation link, he is actually confirmed.
Now, this actually works when I test it manually. In addition, as you can see there is a puts @user.confirmed line that I put in the confirm method to debug this, and it actually prints true when I run the test. However, the test itself fails.
It seems as if the confirmed attribute in my user model isn't being remembered by the test after executing the controller method.
What am I missing? Thanks so much in advance.
Test:
it "should allow a user to be confirmed after clicking confirmation link" do
fill_in('user_email', :with => '[email protected]')
click_button('Submit')
@user = User.find_by_email('[email protected]')
@user.confirmed.should be_false
visit confirm_path(@user.confirmation_code)
@user.confirmed.should be_true
end
Controller method:
def confirm
@confirmation_code = params[:confirmation_code]
@user = User.find_by_confirmation_code(@confirmation_code)
@website = @user.website
@user.confirm
if @user.referrer_id
User.find(@user.referrer_id).increment_signups
end
flash[:success] = "Thanks for signing up!"
flash[:user_show] = @user.id
puts @user.confirmed
redirect_to "http://" + @website.domain_name
end
User model method:
def confirm
self.confirmed = true
self.save
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
访问confirm_path后是否需要重新加载用户对象?试试这个:
或者你可以使用
@user.reload
。测试中引用的用户对象只是应用程序操作的对象的副本,因此它不会自动刷新。您需要再次从数据库中获取它以获取更新的值。
Is it that you would need to reload the user object after visiting the confirm_path? Try this:
Alternatively you could use
@user.reload
.The user object referred to in your test is just a copy of the object being manipulated by the application so it is not going to be automatically refreshed. You need to fetch it from the database a second time to get the updated values.