Rpsec tests failed with 'rspec' but not with 'rspec path_of_the_file'

梦想与她 提交于 2019-12-06 02:23:08

When the execution of an individual spec succeeds but the same spec fails when run as part of the larger suite, it indicates that the prior execution of the other tests is affecting the result.

If the spec in question is deeply nested, as in this case, one way to isolate the problem is to run all the specs in successive directories up from the spec in question until you cause a failure. Once you hit the directory that causes the problem, then you can isolate further by specifying different series of specs to run prior to the failing test until you isolate the problematic spec.

For example, in this case, you would run rspec spec/requests/api/v1 and if that succeeds, rspec spec/requests/api, and if that succeeds rspec spec/requests.

Since under normal conditions RSpec is careful to rollback any changes that individual tests make (both to the Ruby runtime and the database), interference is usually due to some code being run outside of the normal RSpec framework. In general, all test code should be included within the describe blocks.

For me I had

  config.before(:all) do
    Rails.application.load_seed # loading seeds
  end

in my rails_helper.rb file which was loading my seeds at the beginning of each file which was causing conflicts.

I changed my code to load the seeds at the beginning of the suite.

   config.before(:suite) do
        DatabaseCleaner.clean_with(:truncation)
        Rails.application.load_seed # loading seeds
   end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!