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
Ruby SASS, Unable to Resolve Dependancies
Good Cucumber Examples in the Wild
Rails: Good Rspec2 Example Usage? (Also: Cucumber, Pickle, Capybara)
Ruby - Can't Modify Frozen String (Typeerror)
Rails 3/Ruby: Activerecord Find Method in Condition Array to Parameters Single Quote Issue
What Does ':Location => ...' and 'Head :Ok' Mean in the 'Respond_To' Format Statement
How to Read a Password from the Command Line in Ruby
Make Rake Task from Gem Available Everywhere
How to Write Negative Loop in Ruby Like For(I=Index; I >= 0; I --)
What's the Right Way to Implement Equality in Ruby
How to Save Unescaped & in Nokogiri Xml
Rails, How to Render a View/Partial in a Model
What's the Difference Between Request.Remote_Ip and Request.Ip in Rails
In Ruby, What Is the Cleanest Way of Obtaining the Index of the Largest Value in an Array
Rails Authentication Across Apps/Servers
Ruby: Create a String from Bytes
Combinatory Method Like Tap, But Able to Return a Different Value
Why Does Ruby Open-Uri's Open Return a Stringio in My Unit Test, But a Fileio in My Controller