我是rails的新手,并开始了一个新项目。我有rspec2和黄瓜与我的测试工作没有问题。但是,如果我尝试添加guard-rspec和guard-cucumber,则会导致我的规范和集成测试失败。虽然,即使安装了防护装置,如果我手动运行rspec和黄瓜,测试都会成功。
我已添加到我的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
然后当我守卫时,我得到了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似乎在某处弄乱我的工厂数据,因为以“the body必须超过50个字符”开头的文本在我的factory_girl工厂定义中。
修改 我发现rake如何调用rspec,而不是后卫。为什么会这样?
耙:
/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
后卫:
/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'
答案 0 :(得分:5)
看起来你在测试之间清理数据的方式不起作用。
您是否可以在之前(:all)调用中创建数据?
Rake spec调用(以及其他内容)rake db:test:prepare,它将删除并重新构建数据库。呼叫守卫不会调用rake db:test:prepare。这就是你在数据库中看到不同状态的原因。
答案 1 :(得分:1)
我有类似的问题&amp;我花了一段时间才弄明白,所以我想我会发帖。我的所有测试都是使用Rspec,但不是后卫,因为数据库状态有些不同。
我好像需要数据库清理工具,但是当我合并代码时
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
它不起作用。我编写了测试,以便在组的开头只有一个干净的数据库。我希望以某种方式清理每组测试之间的数据库,但我无法弄清楚如何实现比'test'更大但比'suite'更小的粒度。我通过更改测试来解决它,以便我使用干净的数据库开始每个测试。
有两个方面的变化 -
我不确定是否有人会遇到相同的情况,但我最初想说的是,如果数据库清理程序不适合您,可能是因为您需要返回并更改您的规范文件。