Tag: rspec

使用Rspec + Capybara在Rails中测试错误页面

在Rails 3.2.9我有自定义错误页面定义如下: # application.rb config.exceptions_app = self.routes # routes.rb match ‘/404’ => ‘errors#not_found’ 哪个效果如预期。 当我在development.rb设置config.consider_all_requests_local = false时,在访问/foo时我得到了not_found视图 但是如何用Rspec + Capybara测试呢? 我试过这个: # /spec/features/not_found_spec.rb require ‘spec_helper’ describe ‘not found page’ do it ‘should respond with 404 page’ do visit ‘/foo’ page.should have_content(‘not found’) end end 当我运行此规范时,我得到: 1) not found page should respond with 404 page Failure/Error: […]

为什么在后钩中添加“sleep 1”会导致此Rspec / Capybara测试通过?

我使用的是rails 4.0.5,rspec 2.14.1,capybara 2.2.1,capybara-webkit 1.1.0和database_cleaner 1.2.0。 我通过以下function测试看到一些奇怪的行为(模拟用户在post上查看评论,将鼠标hover在图标上以显示菜单,然后单击菜单项以删除评论): let(:user){create(:user)} let(:post){create(:post, author: user)} let!(:comment){create(:comment, post: post, author: user)} … it “can delete a comment” do assert(page.has_css? “#comment-#{comment.id}”) find(“#comment-#{comment.id}-controls”).trigger(:mouseover) find(“#comment-#{comment.id} .comment-delete a”).click assert(page.has_no_css? “#comment-#{comment.id}”) end 这个测试大约80%的时间都失败了,总是由于某些记录从数据库中检索为nil – 我得到NoMethodError: undefined method X for nil:NilClass ,对于各种X值。有时nil是正在评论的删除,有时它是评论附加的post,有时它是评论/post的作者。 如果我在测试结束时添加sleep 1 ,它会通过: it “can delete its own comment” do assert(page.has_css? “#comment-#{comment.id}”) find(“#comment-#{comment.id}-controls”).trigger(:mouseover) find(“#comment-#{comment.id} […]

如何运行单个RSpec测试?

我有以下文件: /spec/controllers/groups_controller_spec.rb 我在终端中使用什么命令来运行该规范以及在哪个目录中运行命令? 我的gem文件: # Test ENVIRONMENT GEMS group :development, :test do gem “autotest” gem “rspec-rails”, “~> 2.4” gem “cucumber-rails”, “>=0.3.2” gem “webrat”, “>=0.7.2” gem ‘factory_girl_rails’ gem ’email_spec’ end 规格文件: require ‘spec_helper’ describe GroupsController do include Devise::TestHelpers describe “GET yourgroups” do it “should be successful and return 3 items” do Rails.logger.info ‘HAIL MARRY’ get :yourgroups, […]

在测试“无限循环”时,最佳做法是什么?

我的基本逻辑是在某处运行无限循环并尽可能地测试它。 拥有无限循环的原因并不重要(游戏的主循环,类似守护进程的逻辑……)而且我更多地询问有关这种情况的最佳实践。 我们以此代码为例: module Blah extend self def run some_initializer_method loop do some_other_method yet_another_method end end end 我想使用Blah.run测试方法Blah.run (我也使用RR ,但普通的rspec是一个可接受的答案)。 我认为最好的方法是分解更多,比如将循环分成另一种方法或其他方法: module Blah extend self def run some_initializer_method do_some_looping end def do_some_looping loop do some_other_method yet_another_method end end end …这允许我们测试run并模拟循环…但是在某些时候需要测试循环内的代码。 那么在这种情况下你会做什么? 只是不测试这个逻辑,意味着测试some_other_method & yet_another_method但不测试do_some_looping ? 通过模拟在某个时刻让循环中断? ……别的什么?