Rspec 当 运行 一个和所有规格时结果不同

Rspec results are different when running one and all specs

这是源代码https://github.com/tenzan/eshop

当我 运行 特定规格时 rspec spec/features/application_controller_spec.rb,测试通过。

但是当我 运行 全部通过执行 rspec 时,它给了我上述规范的错误:

Failures:

  1) Admin::ApplicationController GET #index returns http success
     Failure/Error: expect(response).to have_http_status(:success)
       expected the response to have a success status code (2xx) but it was 302
     # ./spec/controllers/admin/application_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.61968 seconds (files took 1.53 seconds to load)
12 examples, 1 failure

Failed examples:

rspec ./spec/controllers/admin/application_controller_spec.rb:6 # Admin::ApplicationController GET #index returns http success

知道为什么会这样吗?

你 运行 一个人的文件不是当你 运行 整个套件时失败的文件 ;-)

rspec spec/features/application_controller_spec.rb --> 完全没问题。

rspec spec/controllers/admin/application_controller_spec.rb:6 --> 在任何情况下都会失败,因为没有经过身份验证的用户。

还有一些提示:您可以在 .rspec 文件中使用 --require rails_helper 而不是 --require spec_helper,这样您就不需要在每个测试文件中都需要它。还有一个捷径:可以使用简单的 describe 而不是 RSpec.describe.