How to Globally Configure Rspec to Keep the '--Color' and '--Format Specdoc' Options Turned On

How do I globally configure RSpec to keep the '--color' and '--format specdoc' options turned on

As you can see in the docs here, the intended use is creating ~/.rspec and in it putting your options, such as --color.

To quickly create an ~/.rspec file with the --color option, just run:

echo '--color' >> ~/.rspec 

Ruby RSpec : No color on output with a Mac

Add the following contents to a .rspec file to your project dir root.

--color

rspec - how can I override the rspec format for one repository

If the project already has a .rspec file in the root with a different formatter, you can add a .rspec-local file and .gitignore it. Within this .rspec-local config file, you can add your own custom options like:

--format progress

This file will override the project .rspec file for all settings.

This will allow you to keep a local, un-checked in file for that project without committing anything to the repo.

You may need to also set the global ignore file name with:

*nix:

git config --global core.excludesfile '~/.gitignore'

Windows:

git config --global core.excludesfile "%USERPROFILE%\.gitignore"

You can show which file is currenrtly used for your current .gitignore file with:

$ git config --global core.excludesfile

If you get no output then you need to add one as detailed above.

Once added, the output from the above command is (e.g.) ~/.gitignore

How to sort test by modification time in Rspec

Just add this configuration to you test_helper/spec_helper/rails_helper:

RSpec.configure do |config|
config.register_ordering(:global) do |items|
items.sort_by { |item| -File.mtime(item.metadata[:absolute_file_path]).to_i }
end
end

Explanation: Rspec lets you to define custom ordering with config.resgister_orderdering. In this case we're overriding the global config. All you have to do is to pass a block where you define your custom ordering function.

  • item.metadata[:absolute_file_path] gets the file path of your test file.
  • File.mtime gets the modification time of a file and the minus sign is to reverse ordering them

how to get rid of the time-duration added to rspec when using --out?

The [33m is not a time duration; it's an ANSI color code and it's how RSpec prints its output in color. RSpec is meant to sense that output isn't going to a TTY and disable the color for this case but it appears that's not working. You can pass --no-color to manually disable it, though.

Is there a way not to generate controller specs by default?

I think you can configure it in application.rb like this:

config.generators do |g|
g.test_framework :rspec,
fixtures: true,
view_specs: false,
helper_specs: true,
routing_specs: false,
controller_specs: false,
request_specs: true
end

RSpec different DateTime format in response than passed attribute

Rails uses ISO 8601 as default format of JSON serialization for Time objects.

Also, it's better not to rely on ActiveRecord time parsing and use same Time object either for the expectation and for creating the record:

let(:current_date) { Time.parse('Thu, 16 Jul 2020 17:08:02 +0200') }

let(:serializer_response) do
{
'data' => [
{
'id' => 1,
'type' => 'percent_progress',
'attributes' => {
'percent_progress' => 0.5,
'started_at' => current_date.utc.as_json,
}
}
],
}


Related Topics



Leave a reply



Submit