为什么 Guard 会导致我的测试失败?

发布于 2024-11-30 06:08:10 字数 7137 浏览 0 评论 0 原文

我是 Rails 新手,正在开始一个新项目。我的 rspec2 和 cucumber 可以毫无问题地进行我的测试。但是,如果我尝试添加guard-rspec 和guard-cucumber,则会导致我的规范和集成测试失败。虽然,即使安装了防护,如果我手动运行 rspec 和 cucumber,测试都会成功。

我已经添加到我的gemfile中:

gem 'guard-rspec'
gem 'guard-cucumber'
gem 'growl', :require => false if RUBY_PLATFORM =~ /darwin/i
gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~ /darwin/i

我跑了:

bundle
guard init rspec
guard init cucumber

然后当我运行guard时,我得到了rspec失败:

Failures:

  1) PostsController GET index assigns all posts as @posts
     Failure/Error: assigns(:posts).should eq([post])

       expected [#<Post id: 14, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 19:02:46">]
            got [#<Post id: 1, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 2, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 3, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 4, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 5, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">]

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -[#<Post id: 14, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 19:02:46">]
       +[#<Post id: 1, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 2, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 3, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 4, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 5, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">]
     # ./spec/controllers/posts_controller_spec.rb:29:in `block (3 levels) in <top (required)>'

并且我得到了黄瓜失败:

Running all features
Disabling profiles...
..F-----------

(::) failed steps (::)

expected there to be content "Test Title" in "Listing posts\n\nTitle\n    Body\n    Created at\n    \n    \n    \n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  ← Previous 1 2 3 4 5 6 Next →\n\nNew Post\n\n\n" (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:107:in `/^(?:|I )should see "([^"]*)"$/'
features/Homepage.feature:17:in `Then I should see "Test Title"'

Failing Scenarios:
cucumber features/Homepage.feature:6 # Scenario: Viewing the index page with paging

1 scenario (1 failed)
14 steps (1 failed, 11 skipped, 2 passed)

似乎Guard在某种程度上弄乱了我的工厂数据,因为以“身体必须”开头的文本超过 50 个字符”位于我的 factory_girl 工厂定义中。

编辑: 我发现 rake 调用 rspec 与 Guard 的方式存在差异。为什么会这样呢?

耙:

/Users/mandreko/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -S 捆绑执行 rspec ./spec/controllers/posts_controller_spec.rb ./spec/controllers/tags_controller_spec.rb ./spec/helpers/posts_helper_spec.rb ./spec/helpers/tags_helper_spec.rb ./spec/models/post_spec.rb ./spec/models/tag_spec.rb ./spec/requests/posts_spec.rb ./spec/requests/tags_spec.rb ./spec/routing/posts_routing_spec.rb ./spec/routing/tags_routing_spec.rb ./spec/views/posts/edit.html.erb_spec.rb ./spec/views/posts/index.html.erb_spec.rb ./spec/views/posts/new.html.erb_spec.rb ./spec/views/posts/show.html.erb_spec.rb ./spec/views/tags/edit.html.erb_spec.rb ./spec/views/tags/index.html.erb_spec.rb ./spec/views/tags/new.html.erb_spec.rb ./spec/views/tags/show.html.erb_spec.rb

守卫:

/Users/mandreko/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -rrubygems -S /Users/mandreko/.rvm/gems/ruby-1.9.2-p290@rails31rc5/gems/rspec-core-2.6.4/bin/rspec --tty '/Users/mandreko/Documents/mattandreko.com/spec/controllers/posts_controller_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/controllers/tags_controller_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/helpers/posts_helper_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/helpers/tags_helper_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/views/posts/edit.html.erb_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/views/posts/index.html.erb_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/views/posts/new.html.erb_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/views/posts/show.html.erb_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/views/tags/edit.html.erb_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/views/tags/index.html.erb_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/views/tags/new.html.erb_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/views/tags/show.html.erb_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/models/post_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/models/tag_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/requests/posts_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/requests/tags_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/routing/posts_routing_spec.rb' '/Users/mandreko/Documents/mattandreko.com/spec/routing/tags_routing_spec.rb'

I'm new to rails, and starting a new project. I have rspec2 and cucumber working with my tests with no problem. However, if I try to add guard-rspec and guard-cucumber, it causes both my specs and integration tests to fail. Although, even with guard installed, if I run rspec and cucumber manually, the tests all succeed.

I've added to my gemfile:

gem 'guard-rspec'
gem 'guard-cucumber'
gem 'growl', :require => false if RUBY_PLATFORM =~ /darwin/i
gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~ /darwin/i

I ran:

bundle
guard init rspec
guard init cucumber

Then when I run guard, I get rspec failing:

Failures:

  1) PostsController GET index assigns all posts as @posts
     Failure/Error: assigns(:posts).should eq([post])

       expected [#<Post id: 14, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 19:02:46">]
            got [#<Post id: 1, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 2, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 3, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 4, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 5, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">]

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -[#<Post id: 14, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 19:02:46">]
       +[#<Post id: 1, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 2, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 3, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 4, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">, #<Post id: 5, title: "Title", body: "The body must be over 50 characters long, or else i...", created_at: "2011-08-16 09:02:26", updated_at: "2011-08-17 18:56:51">]
     # ./spec/controllers/posts_controller_spec.rb:29:in `block (3 levels) in <top (required)>'

And I get cucumber failing:

Running all features
Disabling profiles...
..F-----------

(::) failed steps (::)

expected there to be content "Test Title" in "Listing posts\n\nTitle\n    Body\n    Created at\n    \n    \n    \n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  Title\n    The body must be over 50 characters long, or else it will fail validation.  This string should be adequate.\n    2011-08-16 09:02:26 UTC\n    Show\n    Edit\n    Destroy\n  ← Previous 1 2 3 4 5 6 Next →\n\nNew Post\n\n\n" (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:107:in `/^(?:|I )should see "([^"]*)"$/'
features/Homepage.feature:17:in `Then I should see "Test Title"'

Failing Scenarios:
cucumber features/Homepage.feature:6 # Scenario: Viewing the index page with paging

1 scenario (1 failed)
14 steps (1 failed, 11 skipped, 2 passed)

It seems like somehow Guard is messing with my factory data somewhere, since the text starting with "The body must be over 50 characters" is in my factory_girl factory definition.

EDIT:
I've found that there are differences in how rake is calling rspec, vs guard. Why would this be?

Rake:

/Users/mandreko/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -S bundle exec
rspec ./spec/controllers/posts_controller_spec.rb
./spec/controllers/tags_controller_spec.rb
./spec/helpers/posts_helper_spec.rb ./spec/helpers/tags_helper_spec.rb
./spec/models/post_spec.rb ./spec/models/tag_spec.rb
./spec/requests/posts_spec.rb ./spec/requests/tags_spec.rb
./spec/routing/posts_routing_spec.rb
./spec/routing/tags_routing_spec.rb
./spec/views/posts/edit.html.erb_spec.rb
./spec/views/posts/index.html.erb_spec.rb
./spec/views/posts/new.html.erb_spec.rb
./spec/views/posts/show.html.erb_spec.rb
./spec/views/tags/edit.html.erb_spec.rb
./spec/views/tags/index.html.erb_spec.rb
./spec/views/tags/new.html.erb_spec.rb
./spec/views/tags/show.html.erb_spec.rb

Guard:

/Users/mandreko/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -rrubygems -S
/Users/mandreko/.rvm/gems/ruby-1.9.2-p290@rails31rc5/gems/rspec-core-2.6.4/bin/rspec
--tty
'/Users/mandreko/Documents/mattandreko.com/spec/controllers/posts_controller_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/controllers/tags_controller_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/helpers/posts_helper_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/helpers/tags_helper_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/views/posts/edit.html.erb_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/views/posts/index.html.erb_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/views/posts/new.html.erb_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/views/posts/show.html.erb_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/views/tags/edit.html.erb_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/views/tags/index.html.erb_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/views/tags/new.html.erb_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/views/tags/show.html.erb_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/models/post_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/models/tag_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/requests/posts_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/requests/tags_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/routing/posts_routing_spec.rb'
'/Users/mandreko/Documents/mattandreko.com/spec/routing/tags_routing_spec.rb'

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

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

发布评论

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

评论(2

流星番茄 2024-12-07 06:08:10

您在测试之间清理数据的方式似乎不起作用。

您是否可能在 before(:all) 调用中创建数据?

Rake 规范调用(除其他外)rake db:test:prepare,它将删除并重新构建您的数据库。调用守卫不会调用 rake db:test:prepare。这就是您在数据库中看到不同状态的原因。

It looks like the way you're cleaning up data between tests isn't working.

Are you perhaps creating data in a before(:all) call?

Rake spec calls (among other things) rake db:test:prepare, which will drop and re-build your database. Calling guard DOES NOT call rake db:test:prepare. That's the reason you're seeing different states in your database.

叹倦 2024-12-07 06:08:10

我有一个类似的问题&我花了一段时间才弄清楚,所以我想我应该发帖。我所有的测试都使用 Rspec,但不使用 Guard,因为数据库状态有些不同。

我似乎需要数据库清理器,但是当我合并代码时

RSpec.configure do |config|
  config.before(:suite) do
       DatabaseCleaner.strategy = :truncation, {:except => %w[topics users]}
       DatabaseCleaner.clean
     end

     config.before(:each) do
       DatabaseCleaner.start
     end

     config.after(:each) do
       DatabaseCleaner.clean
     end

它不起作用。我编写了测试,以便在组的开头只有一个干净的数据库。我希望以某种方式清理每组测试之间的数据库,但我不知道如何实现大于“测试”但小于“套件”的粒度。我通过更改测试来解决这个问题,以便我用干净的数据库开始每个测试。

有两个方面的变化 -

  1. 我必须将调用移至之前的 FactoryGirl 中
    语句
  2. 我必须使用“let”语句来查找数据 by_name
    或其他一些字段,以便我可以验证页面上的数据。

我不确定是否有人会遇到同样的情况,但我主要想说的是,如果数据库清理器不适合您,可能是因为您需要返回并更改您的规范文件。

I had a similar problem & it took a while for me to figure out, so I thought I'd post. All my tests were working with Rspec, but not guard, because the database state was somehow different.

I seemed like I needed the Database Cleaner, but when I incorporated code

RSpec.configure do |config|
  config.before(:suite) do
       DatabaseCleaner.strategy = :truncation, {:except => %w[topics users]}
       DatabaseCleaner.clean
     end

     config.before(:each) do
       DatabaseCleaner.start
     end

     config.after(:each) do
       DatabaseCleaner.clean
     end

it didn't work. I had written my tests so that I only had a clean database at the beginning of a group. I was hoping to somehow clean the db between each group of tests, but I could not figure out how to achieve any granularity bigger than 'test' but smaller than 'suite.' I solved it by changing my tests so that I start every test with a clean database.

There were two areas of changes --

  1. I had to move the calls to FactoryGirl inside the before
    statement
  2. I had to use 'let' statements to find the data by_name
    or some other field so that I could validate the data on the page.

I am not sure if anyone will have the same situation, but I primarly wanted to say that if the Database Cleaner doesn't work for you, it may be because you need to go back and change your spec files.

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