开发者

Why does guard cause my tests to fail?

开发者 https://www.devze.com 2023-03-29 00:26 出处:网络
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 inte

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开发者_Python百科: "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'


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.


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.

rspec2databasehelperguard

0

精彩评论

暂无评论...
验证码 换一张
取 消