【发布时间】:2013-08-13 22:13:20
【问题描述】:
我正在关注 Michael Hartl 的 Ruby on Rails 教程 (railstutorial.org)。
在某些时候,我厌倦了测试失败,因为测试使用了旧的缓存版本的类,所以我在测试环境中关闭了 config.cache_classes。这解决了问题,并且在一段时间内一切顺利。
直到我尝试在第 8.4.3 章中实现集成测试。此时将数据输入到数据库中
it "should make a new user" do
lambda do
visit signup_path
fill_in "Name", :with => "Example User"
fill_in "Email", :with => "user@example.com"
fill_in "Password", :with => "foobar"
fill_in "Confirmation", :with => "foobar"
click_button
response.should have_selector("div.flash.success",
:content => "Welcome")
response.should render_template('users/show')
end.should change(User, :count).by(1)
end
每次测试后都会保留在数据库中,因此只有第一次运行该测试时它才会起作用,之后它总是会失败,直到我手动清空数据库。 除此之外,它还有效。 但现在在第 9 章,集成测试再次失败:
describe "when signed in" do
before(:each) do
@user = Factory(:user)
visit signin_path
fill_in :email, :with => @user.email
fill_in :password, :with => @user.password
click_button
end
it "should have a signout link" do
visit root_path
response.should have_selector("a", :href => signout_path,
:content => "Sign out")
end
这一次它不起作用,用户没有登录,结果页面没有退出链接,只有正常的登录链接。 在网络浏览器中进行测试时,它可以正常工作。
我花了几个小时和几天的时间搜索互联网并测试不同的东西,最后我找到了解决方案:重新打开 config.cache_classes。 现在它可以完美运行了。
那么谁能向我解释为什么 config.cache_classes 会导致测试失败?我怎样才能在不搞乱我的测试的情况下关闭缓存?
提前致谢,
最好的问候,托拜厄斯
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3