autotest 和 rspec 给出不同的答案

发布于 2024-11-19 15:55:29 字数 849 浏览 2 评论 0原文

我有一个应用程序,正在使用 rspec2 (2.6.4)、水豚和硒进行测试。

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

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

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

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

用户由factory-girl创建并使用devise登录,如下所示:

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

after(:each){
    logout
  }

在规范助手中我有

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

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

  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'

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-11-26 15:55:30

解决了,有点。这里到底发生了什么仍然是个谜,但这里是问题的真正含义以及我如何解决它的准确总结。自动测试不是罪魁祸首 - 问题是在一批中运行所有测试。运行 rspec spec/**/* 会导致相同的错误。这是一个线索,表明问题与测试之间未能正确清理数据库有关。我正在使用设备,因此最终依赖 Warden 进行基于机架的身份验证。根据 Warden 文档,我调用了 Warden.test_reset!在 after(:suite) 块中。如果我将其移至 after(:all) 块,则无论单独运行、作为请求测试还是在一个块中运行所有测试,测试都会通过相同的测试。

那么我们学到了什么?我认为这个问题(至少对我来说)最终是由 rspec 挂钩的混乱命名引起的。我认为酒店的“套房”房间少于酒店的“所有”房间。但显然,rspec 示例的“套件”比“所有”示例更多。事实上,一套 rspec 示例只是“spec 目录下的所有示例”。在它们全部运行后清理典狱长没有效果。我们需要在每个规范之后清理它们 - 为此需要运行 test_reset!在 after(:all) 块中。

希望这对某人有帮助...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文