Does .rspec file get run every time rspec runs? - ruby

Does the .rspec file you get when initializing the project, get run every time you run your tests (through rspec [file location]) ? Just curious because inside the .rspec file, using cat .rspec, I found that it contains require spec_helper. If it does get run every time, this would save me a lot of time from writing require spec_helper in each of my test files.

Yes. It gets called every time. You don't need to include it in every spec file.
I have --require spec_helper in my .rspec file.
Here's a link to the rspec docs, which includes a list of other options.

Related

How to use multiple rspec_helpers

We are experimenting with adding integration tests with rspec into our workflow, and are experimenting the following folder structure:
.
├── spec #unit tests
├── spec-integration #e2e tests
When I run the e2e tests with bundle exec rspec SPECS=spec-integration, the --require spec_helper from .rspec automatically goes to spec/spec_helper. This makes sense and expected, but we don't want to use the spec_helper from the unit tests, we want to use another one for integration tests.
How do I tell rspec to go to spec-integration/spec_handler instead of spec/spec_handler as a default?
(Edit/PS: Comments from a folder architecture or suggestions on a better way to design this are also welcome, as we are pretty new to the Ruby ecosystem)
Thanks!
You can override that by specifying --options command line flag. (ref)
bundle exec rspec --default-path spec-integration --options spec_handler
Also, when you use --default-path, you can have a spec-integration/spec_helper.rb and it will use that(because of --require spec_helper in .rspec).
(This is not working for me on rspec 3.9 though - bundle exec rspec SPECS=spec-integration.)
Coming to the folder structure part, I would keep integration specs in a sub-folder inside spec. Both unit and integration specs can have their own helper files and spec/spec_helper can have common configuration.

Ruby - rspec how to include specs in root of project?

rspec always finds files in the spec/ directory tree.
How can I have it also find files that are in the root of the project (the one that contains the spec/ folder iself.
For example if I have a small project with only two tests, any folders may essentially be unneeded overhead given a minimalist approach. Or if you only have one type of test and would only create one folder it may not (or may) be worth creating that one folder or just omitting it altogether. The value it adds of course is immediate description of what type of tests exist, even if only one type exist.
Spec supports a --default-path option, and you can put a default set of command-line options into an .rspec file. That is, you can create a .rspec file in your project root directory containing
--default-path .
My experience has generally been that rspec tests are almost always in a spec directory; I would be a little surprised to see them in a top-level directory. This in particular mirrors the standard gem filesystem layout which puts all library code into a lib directory and tests in a parallel spec directory.
One approach:
# File: spec_helper.rb Add the following at the top:
Dir.glob(File.expand_path("../../*_spec.rb", __FILE__)).each do |file|
require file
end
This will include files at the root to the ones already being included in spec/

Rspec multiple spec folders

Im currently building a project using Rspec in which I sepparated in two different folders with domain code and infrastructure code. Both folders have their own specs in a spec folder. The domain/spec folder is the one containing the spec_helper.rb file, thats required from the tests inside the other folder infrastructure/spec
I'd like to know how to have a spec folder in the root of the project, including the spec_helper file and also tests, and being able to run all the tests with just one command (right now I do it running rspec domain/ infrastructure/)
RSpec is designed to work with all tests in one folder. By default, this folder is called spec/, but you can use a different name with the --default-path option.
So, your options as I see it are:
Edit the source code of rspec-core to let that configuration support multiple directories. Hopefully your PR will be approved and merged.
Or, write a simple wrapper script that runs rspec against both directories. For example, you could alias rspecs='rspec domain/ infrastructure/'.
Or (what I would recommend!), you could just restructure your tests slightly to use spec/domain/ and spec/infrastructure/ folders -- and then everything will just work, by convention, out of the box.

Using SimpleCov in a Ruby Project

I am trying to use the simplecov gem in a Ruby project. However I have failed miserably. Here is what I've done until now.
My project structure is:
ProjectFolder
- lib
- test
I have my tests in test and source code in lib. I have created a test_helper.rb in the test directory and added the following.
require 'simplecov'
SimpleCov.start
Then I put `require 'test/test_helper.rb' in every test file. What happened was it sometimes created some report, and sometimes didn't. And when it did it was inconsistent.
All the tutorials I found was for Rails so I turn to StackOverflow once more, to show me the way.
Rcov/SimpleCov will only report the coverage of the tests ran.
For a full coverage report, you must ensure that the full test suite is ran as the last test, to build a full coverage report.
You will also want to make sure it is the first require in your test_helper.rb file.
From the documentation:
Note: If SimpleCov starts after your application code is already
loaded (via require), it won't be able to track your files and their
coverage! The SimpleCov.start must be issued before any of your
application code is required!

override --fail-fast in rspec

I run specs in my local machine and in a CI server. In the first case I want specs to fail fast, and in the other I want run all of them. I've set --fail-fast in my .rspec file. How can I override it on the CI server? I run there RSpec using rake spec and I set SPEC_OPTS env variable.
The simplest answer is to remove .rspec from your repo. It is used only by you and any other contributor don't need to see that you are using NyanCat to see how your test's are doing.
You can add a .rspec file in your home directory so that the settings only apply to your local machine, and then the .rspec in the project directory will override any settings there.
--no-fail-fast has since been added.
https://relishapp.com/rspec/rspec-core/docs/command-line/fail-fast-option#using-%60--no-fail-fast%60

Resources