Rails Rspec Suite 出现故障,但单独运行时可以通过

cjn*_*cjn 5 rspec ruby-on-rails ruby-on-rails-4

事实:

\n\n
    \n
  1. 运行我的整套规范将导致 610 个规范中出现 21 个一致错误。
  2. \n
  3. 如果我运行任何单独的规范文件(例如:messages_controller_spec.rb),它们都会通过。
  4. \n
  5. 如果我单独运行任何失败的规范,它们都会通过。
  6. \n
\n\n

这些错误大多是 ActionMailer 故障,但也有一些是其他错误。\n一个令人困惑的方面是,某些规范因数据库中比预期多了一行而失败,而另一些规范则因比预期少一行而失败。也就是说,如果是清理或缓存问题,似乎应该始终多一或少一。

\n\n

I\xe2\x80\x99m 当前通过内联运行 Rails 4.1.1、Ruby 2.0.0p451、Rspec 2.14.8、Sidekiq!

\n\n

Gemfile(测试用)

\n\n
group :development, :test do\n  gem \'better_errors\'\n  gem \'binding_of_caller\'\n  gem \'faker\'\n  gem \'guard-rspec\'\n  gem \'pry\'\n  gem \'rspec-rails\'\n  gem \'spork-rails\'\n  gem \'sqlite3\'\n  gem \'thin\'\nend\n\ngroup :test do\n  gem \'capybara\'\n  gem \'capybara-email\'\n  gem \'capybara-webkit\'\n  gem \'database_cleaner\'\n  gem \'fabrication\'\n  gem \'launchy\'\n  gem \'selenium-webdriver\'\n  gem \'shoulda-matchers\'\n  gem \'webmock\'\n  gem \'vcr\'\nend\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,我\xe2\x80\x99已经检查了大约十几个类似的问题,但这些问题无法解决这个问题。所以,澄清一下:

\n\n
    \n
  1. I\xe2\x80\x99m 不使用 ARGV
  2. \n
  3. I\xe2\x80\x99m 不使用 before(:all) - 我使用 before(:each)。
  4. \n
  5. 我在 :suite 和 :each 规范之前尝试过 Rails.cache.clear 。
  6. \n
  7. 我将数据库清理器设置为:截断以进行所有清理(比:交易更慢,但结果更好 - 使用:交易重新加载更新值时遇到问题)
  8. \n
\n\n

为了方便起见,也许一些例子会有所帮助:

\n\n

Scheduler_spec.rb(显示整个套件运行时失败的测试)

\n\n
require \'spec_helper\'\nrequire \'rake\'\nrequire \'sidekiq/testing\'\nSidekiq::Testing.inline!\n\ndescribe "scheduler", :vcr do\n  describe ":wipe_abandoned_images" do\n    let!(:abandoned_old_image)   { Fabricate(:image) }\n    let!(:abandoned_young_image) { Fabricate(:image) }\n    let!(:adopted_image)         { Fabricate(:image) }\n    let(:run_cleaner) do\n      Rake::Task[:wipe_abandoned_images].reenable\n      Rake.application.invoke_task :wipe_abandoned_images\n    end\n\n  before do\n      abandoned_young_image.update_columns(listing_id: nil, updated_at: 6.days.ago)\n      abandoned_old_image.update_columns(  listing_id: nil, updated_at: 9.days.ago)\n      Rake.application.rake_require \'tasks/scheduler\'\n      Rake::Task.define_task(:environment) #Stub env. Rspec runs the App, so dont want Rake to run it again.\n  end\n\n  context "for claimed images" do\n      it "leaves the image" do\n        adopted_image_id = adopted_image.id\n        run_cleaner\n          expect(Image.all.count                ).to eq(2)\n          expect(Image.find(adopted_image_id) ).to be_present\n      end\n    end\n  end\nend\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,我正在使用 Sidekiq 的内联!在以前的应用程序上测试配置并取得良好成功并且没有此问题。

\n\n

got_passwords_controller_spec.rb(显示整个套件运行时失败的测试)

\n\n
require \'spec_helper\'\nrequire \'sidekiq/testing\'\nSidekiq::Testing.inline!\n\ndescribe ForgotPasswordsController do\n  let!(:jen) { Fabricate(:user, email: \'jen@example.com\') }\n\n  describe "POST create" do\n    context "with valid email provided" do\n      before { post :create, email: \'jen@example.com\' }\n      after do\n        ActionMailer::Base.deliveries.clear\n        Sidekiq::Worker.clear_all\n      end\n\n      it \'sends the reset email to the users provided email\' do\n        expect(ActionMailer::Base.deliveries.count).to eq(1)\n      end\n    end\n  end\nend\n
Run Code Online (Sandbox Code Playgroud)\n\n

以下是我以各种方式运行规范时发生的情况:

\n\n

ForgotPasswordsController 规范通过 SublimeText2 中的 RubyTest 插件传递

\n\n
\n

2014-08-28T03:42:43Z 32968 TID-ov5g65p44 INFO: Sidekiq 客户端,带有 \n redis 选项 {} .......... 在 0.95479 秒内完成 11 个示例,\n 0 次失败 使用种子 40226 随机化【5.8秒完成】

\n
\n\n

调度程序测试通过 SublimeText2 中的 RubyTest 通过

\n\n
\n

.向用户分配邀请...完成。.向用户分配\n邀请...完成。..正在扫描服务器中是否有废弃\n 图像... 2014-08-28T01:49:02Z 32426 TID-owjt9ggh8 INFO: Sidekiq\n 客户端已完成 redis options {}。.清理服务器中是否有废弃的图像...完成。.正在清理服务器中是否有废弃的图像...已完成。\n .

\n\n

在 1.52 秒内完成 7 个示例,0 次失败 使用种子随机化\n 37996 [在 8.6 秒内完成]

\n
\n\n

测试通过控制台中的 rspec \n$ rspec ./spec/lib/tasks/scheduler_spec.rb

\n\n
\n

.向用户分配邀请...完成。.向用户分配\n邀请...完成。..正在扫描服务器中是否有废弃\n 图像... 2014-08-28T02:14:43Z 32456 TID-ouiui9g8c INFO: Sidekiq\n 客户端已完成 redis options {}。.清理服务器中是否有废弃的图像...完成。.正在清理服务器中是否有废弃的图像...已完成。\n .

\n\n

在 1.32 秒内完成 7 个示例,0 次失败 使用种子随机\n 19172

\n
\n\n

运行整个 Rspec 套件时测试失败\n如果单独运行或作为它们来自的规范文件运行,这些失败将通过。\n$ 规范

\n\n
\n

在 49.71 秒内完成 610 个示例,21 个失败,10 个待处理

\n
\n\n

失败的例子:

\n\n

有时还有额外的价值

\n\n
\n

13) 调度程序 :wipe_abandoned_images 对于 1\n 周以下的废弃图像留下图像\n 失败/错误:expect(Image.all.count ).to eq(2)

\n\n
   expected: 2\n        got: 3\n\n   (compared using ==)\n # ./spec/lib/tasks/scheduler_spec.rb:78:in `block (4 levels) in <top (required)>\'\n
Run Code Online (Sandbox Code Playgroud)\n
\n\n

有时它会丢失或不\xe2\x80\x99t加载值

\n\n
\n

18) ForgotPasswordsController POST 使用提供的有效电子邮件创建\n 将重置电子邮件发送到用户提供的电子邮件\n 失败/错误:expect(ActionMailer::Base.deliveries.count).to eq(1)

\n\n
   expected: 1\n        got: 0\n\n   (compared using ==)\n # ./spec/controllers/forgot_passwords_controller_spec.rb:22:in `block (4 levels) in <top (required)>\'\n
Run Code Online (Sandbox Code Playgroud)\n
\n\n

这是失败的列表

\n\n
rspec ./spec/controllers/messages_controller_spec.rb:161 # MessagesController POST create message about listing to user from guest with valid information with EXISTING, UN-confirmed guest with EXPIRED token sends another confirmation email with link to the guest\nrspec ./spec/controllers/messages_controller_spec.rb:114 # MessagesController POST create message about listing to user from guest with valid information with NEW, UN-confirmed, and valid guest email sends an invitation for the guest to be put on safe-email list\nrspec ./spec/controllers/invitations_controller_spec.rb:30 # InvitationsController POST create with valid email & available invitations sends an email\nrspec ./spec/controllers/invitations_controller_spec.rb:33 # InvitationsController POST create with valid email & available invitations sends an email to the recipient_email address\nrspec ./spec/controllers/users_controller_spec.rb:161 # UsersController POST create with invitation token in params with valid token & input confirmation email sending sends the email to the registering user\nrspec ./spec/controllers/users_controller_spec.rb:158 # UsersController POST create with invitation token in params with valid token & input confirmation email sending sends the email\nrspec ./spec/controllers/users_controller_spec.rb:164 # UsersController POST create with invitation token in params with valid token & input confirmation email sending sends an email with a confirmation link in the body\nrspec ./spec/controllers/users_controller_spec.rb:354 # UsersController GET confirm_with_token with valid token has a welcome message in the email\nrspec ./spec/controllers/users_controller_spec.rb:348 # UsersController GET confirm_with_token with valid token sends a welcome email\nrspec ./spec/controllers/users_controller_spec.rb:351 # UsersController GET confirm_with_token with valid token sends the welcome email to the user\nrspec ./spec/controllers/searches_controller_spec.rb:19 # SearchesController GET search GET search with specific category selected returns the matching OR partial-matching table row objects\nrspec ./spec/controllers/searches_controller_spec.rb:22 # SearchesController GET search GET search with specific category selected only returns values from the selected category\nrspec ./spec/lib/tasks/scheduler_spec.rb:75 # scheduler :wipe_abandoned_images for abandoned images under 1 week old leaves the image\nrspec ./spec/lib/tasks/scheduler_spec.rb:68 # scheduler :wipe_abandoned_images for abandoned images over 1 week old deletes the images\nrspec ./spec/lib/tasks/scheduler_spec.rb:84 # scheduler :wipe_abandoned_images for claimed images leaves the image\nrspec ./spec/controllers/forgot_passwords_controller_spec.rb:24 # ForgotPasswordsController POST create with valid email provided sets the email subject to notify the user of the reset link\nrspec ./spec/controllers/forgot_passwords_controller_spec.rb:27 # ForgotPasswordsController POST create with valid email provided sends the link with token in the body of the email\nrspec ./spec/controllers/forgot_passwords_controller_spec.rb:21 # ForgotPasswordsController POST create with valid email provided sends the reset email to the users provided email\nrspec ./spec/controllers/reset_passwords_controller_spec.rb:70 # ResetPasswordsController POST create with a valid token sets the email subject to notify the user of the reset password\nrspec ./spec/controllers/reset_passwords_controller_spec.rb:67 # ResetPasswordsController POST create with a valid token sends a confirmation email to the user that their password has been changed\nrspec ./spec/controllers/reset_passwords_controller_spec.rb:73 # ResetPasswordsController POST create with a valid token sends the link with token in the body of the email\n
Run Code Online (Sandbox Code Playgroud)\n

dre*_*-hh 1

我无法解释真正导致错误的原因。但这一定是由于全局设置了 Sidekiq 测试模式。从规范的头部删除 Sidekiq 设置并尝试以下操作:

    before do 
    Sidekiq::Testing.inline! do
      post :create, email: 'jen@example.com' 
     end  
    end
    after do
      ActionMailer::Base.deliveries.clear
      Sidekiq::Worker.clear_all
    end

    it 'sends the reset email to the users provided email' do
      expect(ActionMailer::Base.deliveries.count).to eq(1)
    end  
Run Code Online (Sandbox Code Playgroud)