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

57,136

Solution 1

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 

Solution 2

One can also use a spec_helper.rb file in all projects. The file should include the following:

RSpec.configure do |config|
  # Use color in STDOUT
  config.color = true

  # Use color not only in STDOUT but also in pagers and files
  config.tty = true

  # Use the specified formatter
  config.formatter = :documentation # :progress, :html,
                                    # :json, CustomFormatterClass
end

Any example file must require the helper to be able to use that options.

Solution 3

In your spec_helper.rb file, include the following option:

RSpec.configure do |config|
  config.color_enabled = true
end

You then must require in each *_spec.rb file that should use that option.

Solution 4

If you use rake to run rspec tests then you can edit spec/spec.opts

http://rspec.info/rails/runners.html

Solution 5

Or simply add alias spec=spec --color --format specdoc to your ~/.bashrc file like me.

Share:
57,136

Related videos on Youtube

Evolve
Author by

Evolve

Freelance Web Developer, Software Craftsman, Rubyist. Code Mentor at @thinkful. CoFounder of @travelbuddies_ winners of #startupADL. Jelly Maker #adjelly.

Updated on November 05, 2020

Comments

  • Evolve
    Evolve over 3 years

    How do I set global configuration for RSpec in Ubuntu.

    Specifically so, --color and --format specdoc stay turned on, across all my projects (ie every time I run rspec anywhere).

  • Søren Debois
    Søren Debois over 13 years
    ./spec.opts has been depricated. Rails 3 wants the file to be named ./.rspec or ~/.rspec
  • Ian Vaughan
    Ian Vaughan over 12 years
    This solution is not very portable. Correct answer is @abyx with using .rspec, as when its checked in with the project, anyone else getting it will get the same settings.
  • zzeroo
    zzeroo over 12 years
    But the question was about "global configuration for RSpec in Ubuntu", "across all my projects" not portable nor coop mode.
  • Ian Vaughan
    Ian Vaughan over 12 years
    Humm, I guess your right, your answer does relate directly to the OP. I was thinking bigger picture, but I still think the better answer was @abyx, if the OP gets used to config projects correctly then others will benefit, maybe not now as he might be workin solo, but good practices are just that. Sorry, bit ranty, just my way of thinking.
  • dwc
    dwc almost 12 years
    @zzeroo @Ian : Do note that putting the .rspec file in the user's home directory (as I mentioned in my answer) works globally for all of the user's invocations of rspec. That is in fact more solid than using aliases, as some gems/other aliases/tools the user might use would not necessarily use the alias
  • Olivier Lacan
    Olivier Lacan almost 12 years
    The original question asks for a global configuration, this is a project-specific one. Useful, but not the correct answer, unlike abyx's which points to the .rspec file.
  • phatmann
    phatmann over 11 years
    A search for info about the RSpec config and formatters brings one to this page, so I appreciated this answer, even if it was for the wrong question :-)
  • Adam Spiers
    Adam Spiers over 11 years
    --tty is also required if you want color even when using pagers.
  • Evolve
    Evolve over 11 years
    Now coming back to this question and being a little older and wiser, I would agree that setting the .rspec dot file is now a better option. Updating my approved answer to abyx, thanks so much zzero for answering my question best back in 2010. :)
  • Ian Vaughan
    Ian Vaughan almost 11 years
    Whats the pros/cons to using .rspec or spec_helper.rb? @shamaoke @christoph
  • Christoph Petschnig
    Christoph Petschnig almost 10 years
    One big advantage of the ~/.rspec solution is the portability. E.g. our CI server does not handle color output very well. With the user dir config file, we can easily adopt to different environments.
  • Nick Gronow
    Nick Gronow almost 10 years
    One other relevant note is that you can create the .rspec file on a project level and have it apply to just that project. Just thought some might want to know that here as well.
  • elado
    elado almost 10 years
    color_enabled is now color
  • Edward Anderson
    Edward Anderson over 9 years
    Moving my configuration options from spec_helper.rb to .rspec resolved this issue for me.
  • BenKoshy
    BenKoshy almost 8 years
    Hi thank you - would you be able to elaborate on the following: "You then must require in each *_spec.rb file that should use that option." i don't understand.
  • Christoph Petschnig
    Christoph Petschnig almost 8 years
    Each of your *_spec.rb file usually starts with require "spec_helper", you should not forget this.
  • vc669
    vc669 over 3 years
    @AdamSpiers what's a pager?