Ruby on Rails 测试失败或成功取决于测试套件的调用方式

Ruby on Rails test fails or succeeds depending on how the test suite is called

我正在学习 Rails 教程(第 4 版)中的 Hartl Ruby,我在尝试进行一些测试时遇到了问题 运行 - 特别是第 11.2.3 节中的练习。到目前为止,我所有的测试都是绿色的。

我测试新的一段代码:

vagrant@ubuntu-18:/vagrant/sample_app/test$ rails test:mailers
Started with run options --seed 59483

  1/1: [======================================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.33304s
1 tests, 9 assertions, 0 failures, 0 errors, 0 skips

一切都是groovy。

我尝试 运行 完整的测试套件:

vagrant@ubuntu-18:/vagrant/sample_app/test$ rails test
Running via Spring preloader in process 11914
Started with run options --seed 56533

ERROR["test_account_activation", UserMailerTest, 1.2505091589991935]
 test_account_activation#UserMailerTest (1.25s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `merge' for nil:NilClass
            app/views/user_mailer/account_activation.html.erb:6:in `_app_views_user_mailer_account_activation_html_erb__2910351700178259135_47130470135880'
            app/mailers/user_mailer.rb:10:in `account_activation'
            test/mailers/user_mailer_test.rb:9:in `block in <class:UserMailerTest>'

  48/48: [========================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.71036s
48 tests, 206 assertions, 0 failures, 1 errors, 0 skips

混蛋。我不会在这里复制的测试确认它绝对是同一段代码,在一个地方失败而在另一个地方工作。

但是,如果我不调用 rails test 而调用 rails test:run,那么会发生这种情况:

vagrant@ubuntu-18:/vagrant/sample_app/test$ rails test:run
Started with run options --seed 50303

  48/48: [========================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.72194s
48 tests, 215 assertions, 0 failures, 0 errors, 0 skips

同样,如果我使用 rake testrake test:run,那么一切都很好。

vagrant@ubuntu-18:/vagrant/sample_app/test$ rake test
(in /vagrant/sample_app)
Started with run options --seed 9429

  48/48: [========================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.70475s
48 tests, 215 assertions, 0 failures, 0 errors, 0 skips
vagrant@ubuntu-18:/vagrant/sample_app/test$ rake test:run
(in /vagrant/sample_app)
Started with run options --seed 50280

  48/48: [========================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.73529s
48 tests, 215 assertions, 0 failures, 0 errors, 0 skips

谁能解释一下这是怎么回事?理想情况下如何让 rails test 调用再次工作?

编辑

使用 rails test:run 使用的相同种子开始 rails test 以表明它与测试排序无关。

vagrant@ubuntu-18:/vagrant/sample_app/test$ rails test:run 
Started with run options --seed 326

  48/48: [=======================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.66690s
48 tests, 215 assertions, 0 failures, 0 errors, 0 skips
vagrant@ubuntu-18:/vagrant/sample_app/test$ rails test --seed 326
Running via Spring preloader in process 28117
Started with run options --seed 326

ERROR["test_account_activation", UserMailerTest, 1.605607868055813]
 test_account_activation#UserMailerTest (1.61s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `merge' for nil:NilClass
            app/views/user_mailer/account_activation.html.erb:6:in `_app_views_user_mailer_account_activation_html_erb__2910351700178259135_47130458820540'
            app/mailers/user_mailer.rb:10:in `account_activation'
            test/mailers/user_mailer_test.rb:9:in `block in <class:UserMailerTest>'

  48/48: [=======================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.64637s
48 tests, 206 assertions, 0 failures, 1 errors, 0 skips

rake TESTOPTS="--seed=326"也成功了。

编辑 2

测试:

require 'test_helper'

class UserMailerTest < ActionMailer::TestCase

  test "account_activation" do
    user = users(:michael)
    user.activation_token = User.new_token
    mail = UserMailer.account_activation(user)
    assert_equal "Account Activation", mail.subject
    assert_equal [user.email], mail.to
    assert_equal ['noreply@example.com'], mail.from
    assert_match user.name, mail.body.encoded
    assert_match user.activation_token, mail.body.encoded
    assert_match CGI.escape(user.email), mail.body.encoded
  end


end

测试方法: (其中以 mail to: 开头的行是 user_mailer.rb:10)

class UserMailer < ApplicationMailer

  def account_activation(user)
    @user = user
    mail to: @user.email, subject: "Account Activation"
  end

end

夹具:

michael:
    name: Michael Example
    email: michael@example.com
    password_digest: <%= User.digest('password') %>
    admin: true
    activated: true
    activated_at: <%= Time.zone.now %>

模板:

<h1>Sample App</h1>

<p>Hi <%= @user.name %>, </p>
<p>Welcome to the Sample App! Please click on the link belowe to activate your account.</p>

<%= link_to "Activate", edit_account_activation_url(@user.activation_token, email: @user.email) %>

感谢@AJFaraday 在评论中指出 Spring 预加载器是 运行。

我打电话给 spring stop,问题自行解决了。

Spring好像又重新开机了,但是rails test现在给出的测试结果和其他三种方法一样。