当我运行我的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次失败,故障似乎与警告有关(至少这是可能的)。看起来某些关联被加载了两次,这导致了规范中的错误。
我该如何调试这种情况?因为似乎有这么多的可能性,所以很难在这里发布更具体的代码片段。但这里是我的GitHub回购的链接,也许有人可以重现它?
答案 0 :(得分:1)
我调试应用程序并发现:
1)您的规格取决于订单。例如
bin/rspec spec --seed 49796 # gives 5 failures
bin/rspec spec --seed 1 # gives 0 failure
2)另外,我将caller
放到User
,我可以看到模型User
被加载两次(你应该停止弹簧看第一次调用)。分析调用者堆栈(对于种子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/rake
,bin/rspec spec 1
,bin/rspec spec --seed 49796
)。
我不确定删除这些行是解决方案,因为我怀疑spring会缓存应用程序,这就是它现在可以工作的原因。也许您可以检查在定义模型文件之前是否定义了类(例如spring本身会检查它)。从另一只手i18n_keys_spec
加载rails_helper
,它应加载包括模型在内的所有应用。
希望它可以帮到你