'Is there a way to keep the order of cases as defined, while running bundle exec rspec {file_path} --format documentation?

I am having a problem while running

bundle exec rspec spec/services/abc_service_spec.rb --format documentation

The output of it, is not in the same order as the order of cases defined in the file. Is there any option to pass with while running rspec command ?



Solution 1:[1]

According to the documentation, Rspec uses the --order option to order the files, groups, and examples. The available ordering options are defined and rand while defined is the default options.

If somehow your Rspec order option has changed, then run the rspec with --order defined like

exec rspec spec/services/abc_service_spec.rb --format documentation --order defined

If you want to set it globally then add below code to your spec_helper.rb

Rspec.configure do |config|
  config.order = :defined
end

Hope you found this helpful.

Solution 2:[2]

https://relishapp.com/rspec/rspec-core/docs/command-line/order

bundle exec rspec spec/services/abc_service_spec.rb --format documentation --order defined

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zamir Manihar
Solution 2 Siim Liiser