为什么 Guard 会导致我的测试失败?
我是 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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在测试之间清理数据的方式似乎不起作用。
您是否可能在 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.
我有一个类似的问题&我花了一段时间才弄清楚,所以我想我应该发帖。我所有的测试都使用 Rspec,但不使用 Guard,因为数据库状态有些不同。
我似乎需要数据库清理器,但是当我合并代码时
它不起作用。我编写了测试,以便在组的开头只有一个干净的数据库。我希望以某种方式清理每组测试之间的数据库,但我不知道如何实现大于“测试”但小于“套件”的粒度。我通过更改测试来解决这个问题,以便我用干净的数据库开始每个测试。
有两个方面的变化 -
语句
或其他一些字段,以便我可以验证页面上的数据。
我不确定是否有人会遇到同样的情况,但我主要想说的是,如果数据库清理器不适合您,可能是因为您需要返回并更改您的规范文件。
rspec2databasehelper守卫
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
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 --
statement
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