运行 rails 规范使用 rake 命令 => 0 失败,而 运行 他们使用 rspec 命令 => 1 警告 5 失败

Running rails specs using rake command => 0 fails, while running them with rspec command => 1 warning 5 fails

当我 运行 我的 Rails 项目的规范时,我通常使用 rake,输出如下:

josh@macbuech:~/Documents/Work/MuheimWebdesign/base/src (master *)$ rake
/Users/josh/.rvm/rubies/ruby-2.1.0/bin/ruby -I/Users/josh/.rvm/gems/ruby-2.1.0@base/gems/rspec-core-3.1.7/lib:/Users/josh/.rvm/gems/ruby-2.1.0@base/gems/rspec-support-3.1.2/lib /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/rspec-core-3.1.7/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb  --color --format Fuubar
Run options: include {:focus=>true}                                             

All examples were filtered out; ignoring {:focus=>true}
 121/121 |======================= 100 ========================>| Time: 00:00:07 

Pending:
  File upload uses an uploaded file (from the temporary cache) after a re-display and then successful submit of the form
    # Seems to be an issue of Carrierwave, see https://github.com/carrierwaveuploader/carrierwave/issues/1414
    # ./spec/features/file_upload_spec.rb:41

Finished in 7.05 seconds (files took 5.82 seconds to load)
121 examples, 0 failures, 1 pending

Top 3 slowest examples (1.76 seconds, 25.0% of total time):
  RegistrationsController POST 'create' valid input creates a guest user and converts it to a registered one
    0.76552 seconds ./spec/controllers/registrations_controller_spec.rb:9
  Signing out signs the user out
    0.53429 seconds ./spec/features/user/sessions/destroy_spec.rb:9
  File upload uses an uploaded file (from the temporary cache) after a re-display and then successful submit of the form
    0.4628 seconds ./spec/features/file_upload_spec.rb:41

Top 3 slowest example groups:
  Signing out
    0.53429 seconds average (0.53429 seconds / 1 example) ./spec/features/user/sessions/destroy_spec.rb:3
  RegistrationsController
    0.41286 seconds average (0.82572 seconds / 2 examples) ./spec/controllers/registrations_controller_spec.rb:3
  File upload
    0.35467 seconds average (2.13 seconds / 6 examples) ./spec/features/file_upload_spec.rb:3

Randomized with seed 56456

0 次失败。

但是当我用 rspec 命令 运行 它时,我得到这个:

josh@macbuech:~/Documents/Work/MuheimWebdesign/base/src (master *)$ rspec
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
......................*.............................................../Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activerecord-4.2.0/lib/active_record/associations.rb:1693: warning: already initialized constant Role::HABTM_Users
/Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activerecord-4.2.0/lib/active_record/associations.rb:1693: warning: previous definition of HABTM_Users was here
/Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activerecord-4.2.0/lib/active_record/associations.rb:1693: warning: already initialized constant User::HABTM_Roles
/Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activerecord-4.2.0/lib/active_record/associations.rb:1693: warning: previous definition of HABTM_Roles was here
......FFFF.............F...........................

Pending:
  File upload uses an uploaded file (from the temporary cache) after a re-display and then successful submit of the form
    # Seems to be an issue of Carrierwave, see https://github.com/carrierwaveuploader/carrierwave/issues/1414
    # ./spec/features/file_upload_spec.rb:41

Failures:

  1) User creating a user validates uniqueness of name
     Failure/Error: expect(@user).to have(1).error_on(:name)
       expected 1 error on :name, got 2
     # ./spec/models/user_spec.rb:90:in `block (3 levels) in <top (required)>'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # -e:1:in `<main>'

  2) User creating a user validates presence of email
     Failure/Error: expect(@user).to have(1).error_on(:email)
       expected 1 error on :email, got 2
     # ./spec/models/user_spec.rb:95:in `block (3 levels) in <top (required)>'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # -e:1:in `<main>'

  3) User creating a user validates presence of password
     Failure/Error: expect(@user).to have(1).error_on(:password)
       expected 1 error on :password, got 2
     # ./spec/models/user_spec.rb:100:in `block (3 levels) in <top (required)>'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # -e:1:in `<main>'

  4) User creating a user validates presence of name
     Failure/Error: expect(@user).to have(1).error_on(:name)
       expected 1 error on :name, got 2
     # ./spec/models/user_spec.rb:83:in `block (3 levels) in <top (required)>'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # -e:1:in `<main>'

  5) User creating a guest validates presence of name
     Failure/Error: expect(@guest).to have(1).error_on(:name)
       expected 1 error on :name, got 2
     # ./spec/models/user_spec.rb:43:in `block (3 levels) in <top (required)>'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # -e:1:in `<main>'

Finished in 7.34 seconds (files took 0.47745 seconds to load)
121 examples, 5 failures, 1 pending

Failed examples:

rspec ./spec/models/user_spec.rb:86 # User creating a user validates uniqueness of name
rspec ./spec/models/user_spec.rb:93 # User creating a user validates presence of email
rspec ./spec/models/user_spec.rb:98 # User creating a user validates presence of password
rspec ./spec/models/user_spec.rb:81 # User creating a user validates presence of name
rspec ./spec/models/user_spec.rb:40 # User creating a guest validates presence of name

Top 3 slowest examples (2.18 seconds, 29.7% of total time):
  Signing up signs up a new user
    1.07 seconds ./spec/features/user/registration/new_spec.rb:4
  I18n offers contents in english
    0.66078 seconds ./spec/features/i18n_spec.rb:10
  File upload displays a preview of an uploaded file (from the temporary cache) after a re-display of the form
    0.44416 seconds ./spec/features/file_upload_spec.rb:27

Top 3 slowest example groups:
  Signing up
    1.07 seconds average (1.07 seconds / 1 example) ./spec/features/user/registration/new_spec.rb:3
  File upload
    0.34402 seconds average (2.06 seconds / 6 examples) ./spec/features/file_upload_spec.rb:3
  I18n
    0.25478 seconds average (0.76435 seconds / 3 examples) ./spec/features/i18n_spec.rb:3

Randomized with seed 49596

1个警告和5个失败,失败似乎与警告有关(至少这是一种可能性)。某些关联似乎被加载了两次,这导致了规范中的错误。

如何调试这种情况?因为似乎有太多的可能性,所以很难 post 更具体的代码片段在这里。但这是我的 GitHub 仓库的 link,也许有人可以复制它?

https://github.com/jmuheim/base

我调试了应用,发现:

1) 您的规格取决于订单。例如

bin/rspec spec --seed 49796 # gives 5 failures
bin/rspec spec --seed 1     # gives 0 failure

2) 我还把 caller 放到 User 上,我可以看到模型 User 被加载了两次(你应该停止 spring 才能看到第一次调用) .分析调用者堆栈(种子 49796)我注意到第二个不同并且有奇怪的行

...
"/home/vaska/projects/base/spec/i18n_keys_spec.rb:84:in `block in project_models'", 
"/home/vaska/projects/base/spec/i18n_keys_spec.rb:83:in `each'", 
"/home/vaska/projects/base/spec/i18n_keys_spec.rb:83:in `project_models'", 
"/home/vaska/projects/base/spec/i18n_keys_spec.rb:61:in `block (3 levels) in <top (required)>'", 
"/home/vaska/projects/base/spec/i18n_keys_spec.rb:60:in `each'", 
"/home/vaska/projects/base/spec/i18n_keys_spec.rb:60:in `block (2 levels) in <top (required)>'",
...

line 第二次加载模型。当我在 i18n_keys_spec 中注释第 86-88 行时,所有三种情况都成功通过(我的意思是 bin/rakebin/rspec spec 1bin/rspec spec --seed 49796)。

我不确定删除这些行是否是解决方案,因为我怀疑 spring 缓存了该应用程序,这就是它现在可以工作的原因。也许您可以检查是否在 require 模型文件之前定义了 class(例如 spring 本身会检查它)。另一方面 i18n_keys_spec 加载 rails_helper 应该加载所有应用程序,包括模型。

希望对你有帮助