autotest和rspec给出不同的答案 [英] autotest and rspec giving different answers

查看:179
本文介绍了autotest和rspec给出不同的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我正在使用rspec2(2.6.4),capybara和selenium进行测试。



当我直接用rspec运行任何请求规范时,测试通过。



当我使用自动测试运行请求规范时,都会失败,并显示一条消息,指出找不到ID = * 的用户。



当自动测试自动重新加载第一个请求测试时,它会通过



自动测试然后重新加载所有测试,当它达到请求测试他们都再次失败



用户正在由工厂女孩创建,并使用devise进行登录,如下所示:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b b b b b b b b b b b b b b b b b b b after(:each){
logout
}

在规范帮助我有

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

这些是我安装的相关宝石(组:测试)

  gemrspec
gemrspec-rails
gemautotest-rails
gemselenium-webdriver,> = 0.2.2
gem'capybara',:git => 'git://github.com/jnicklas/capybara.git'
gem'launchy'
gem'database_cleaner'

这已经让我困惑了一段时间 - 任何想法的人?甚至有关如何开始查看堆栈跟踪以查看两个调用不同的任何想法?

解决方案

解决方案。这里真正发生的一切仍然是一个谜,但这里是一个准确的总结,问题真的是什么,以及我如何解决。自动测试不是罪魁祸首 - 问题是在一个批次中运行所有的测试。运行 rspec spec / ** / * 导致相同的错误。这是一个线索,问题是在测试之间无法正确清理数据库。我正在使用devise,所以最终依靠监狱长进行机架认证。根据Warden的文档,我打电话给Warden.test_reset!在after(:suite)块中。如果我将它移动到after(:all)块,则测试通过相同,无论是单独运行,作为请求测试还是在一个块中的所有测试。



那么我们学到了什么?我认为问题(至少对我来说)最终是由于rspec钩子的混乱命名造成的。我想到酒店房间的套房不如酒店的全部房间。但显然,rspec示例的套件不仅仅是所有的例子。实际上,一套rspec示例只是spec目录下的所有示例。清理监狱长全部运行后无效。我们需要在每个规范之后清理它们,并且需要运行test_reset!在后((全部))中。



希望这有助于某人...


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?

解决方案

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 ...

这篇关于autotest和rspec给出不同的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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