autotest and rspec giving different answers

爷,独闯天下 提交于 2019-12-11 02:42:56

问题


I have an application that I'm testing with rspec2 (2.6.4), capybara and selenium.

When I run any request spec directly with rspec, the test passes.

When I run the request specs with autotest they all fail with a message saying 'Cannot find a User with ID= **.

When autotest reloads the first request test automatically, it passes

Autotest then reloads all tests and when it reaches the request tests they all fail again

Users are being created by factory-girl and logged in using devise as follows:

before(:each) do
  @user = Factory(:user)   
  login_as @user
end 

after(:each){
    logout
  }

and in the spec helper I have

def login(user)
  post login_path, :login => user.login, :password => 'testing'
end

These are the relevant gems i have installed (group :test)

  gem "rspec"
  gem "rspec-rails"
  gem  "autotest-rails"
  gem "selenium-webdriver", ">= 0.2.2"
  gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
  gem 'launchy'
  gem 'database_cleaner'

This has been baffling me for a while now - any thoughts anyone? Even any thoughts on how to start looking at the stack trace to see where the two calls are differing?


回答1:


Solved, sort of. What is really going on here remains a mystery, but here is an accurate summary of what the problem really is, and how I resolved it. Autotest is not the culprit - the problem is running all the tests in one batch. Running rspec spec/**/* causes the same error. This is a clue that the issue is to do with a failure to clean up the database properly between tests. I am using devise, and so am ultimately relying on warden for rack-based authentication. Following the Warden documentation I put a call to Warden.test_reset! in the after(:suite) block. If I move this to the after(:all) block, the tests pass the same whether run individually, as request tests, or all the tests in one block.

So what did we learn? I think the problem (for me at least) was ultimately caused by a confusing naming of the rspec hooks. I think of a "suite" of rooms in a hotel as being less than "all" the rooms in a hotel. But apparently a "suite" of rspec examples is more than "all" the examples. In fact a suite of rspec examples is just "all the examples under the spec directory". Cleaning up warden after they have all been run has no effect. We need to clean them up after each spec - and to do that need to run test_reset! in the after(:all) block.

Hope this helps someone ...



来源:https://stackoverflow.com/questions/6656104/autotest-and-rspec-giving-different-answers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!