config.cache_classes = false 弄乱了 rspec 测试? [英] config.cache_classes = false messing up rspec tests?

查看:22
本文介绍了config.cache_classes = false 弄乱了 rspec 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Michael Hartl (railstutorial.org) 的 Ruby on Rails 教程.

I'm following the Ruby on Rails Tutorial by Michael Hartl (railstutorial.org).

在某些时候,我厌倦了测试失败只是因为测试使用了旧的缓存版本的类,所以我在测试环境中关闭了 config.cache_classes.这解决了问题,一段时间内一切顺利.

At some point I got tired of tests failing just bescause the tests used old cached versions of classes, so I turned off config.cache_classes in the test environment. That fixed the issue and everything went good for some time.

直到我尝试实现第 8.4.3 章中的集成测试.此时数据用

Until I tried implementing the Integration tests in Chapter 8.4.3. At this point the data entered into the database with

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 章,集成测试再次失败:

would remain in the Database after each test, so only the first time this test ran it would work, after that it always fails until i manually empty the database. Apart from that it worked. But now in chapter 9, again the integration test fails:

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

这次不起作用,用户没有登录,结果页面没有退出链接,只有正常的登录链接.在网络浏览器中测试时,它工作正常.

This time it just doesn't work, the user is not getting logged in and the resulting page has no sign out link, just the normal sign in link. When testing this in a webbrowser it works fine.

我花了数小时和数天在互联网上搜索并测试不同的东西,最后我找到了解决方案:重新打开 config.cache_classes.现在它可以完美运行.

It took me hours and days of searching the internet and testing different stuff and finally I found the solution: Turning config.cache_classes back on. Now it works flawlessly.

那么谁能向我解释为什么 config.cache_classes 使测试失败?我怎样才能在不搞乱我的测试的情况下关闭缓存?

So can anyone explain to me why config.cache_classes makes the tests fail? And how can I turn off caching without messing up my tests?

提前致谢,

最好的问候,托比亚斯

推荐答案

当您进行 Capybara 调用时,它会使用 rack-test 来模拟对 rails 应用程序的调用.每次调用完成时,它都会重新加载所有 rails 类.这意味着您在调用 'visit signin_path' 之前创建的 @user 对象将被清除,因为所有 ActiveRecord 对象都已重新加载.

When you make a Capybara call, it uses rack-test to emulate a call to the rails app. Every time a call is completed, it reloads all rails classes. What this means is that the @user object you created before you called 'visit signin_path' gets nil'd out because all ActiveRecord objects have been reloaded.

当您将 cache-classes 设置为 true 时,它​​会告诉 Rack 不要在每次请求时重新加载 ActiveRecord 对象,以便您的测试再次通过.

When you set cache-classes to true, it tells Rack not to reload the ActiveRecord objects on every request, so your tests pass again.

我相信,如果您希望在不打开缓存类的情况下通过上面编写的测试,您应该将 '@user = Factory(:user)' 行移到 'visit signin_path' 行下方.

I believe that if you want the test you wrote above to pass without turning on cache-classes, you should move the '@user = Factory(:user)' line below the 'visit signin_path' line.

这篇关于config.cache_classes = false 弄乱了 rspec 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆