This is part of my spec_helper.rb
:
RSpec.configure do |config|
config.before(:each) do
login(email, password)
visit root_url
end
end
that I need in all of my (20+) tests except one.
Is there a way to avoid that single test to execute the before hook
?
You can add metadata to tests that do not need to login, then evaluate that metadata in your
before
hook.For example, two tests in the same file. One needs to login and one does not.
So we added metadata to our
it
block. Next, we configure thebefore
hook to evaluate the example's metadata.Now, each test will
visit root_url
but only the ones not tagged with:logged_out
will calllogin
.RSpec calls these metadata based hooks filters. You can learn a bit more about them here.
Another option is to continue using the configuration in
spec_helper.rb
and when the test starts in the spec where I don't need it, doPutting this kind of code into spec helper seems a bit odd. Feature specs is all you have? No unit tests? Even if it's the former, copy that code into individual specs that need it. If they're all in the same file, you can use contexts to prevent some duplication.
Global rspec config in spec_helper.rb is meant for other things. Ones that make sense for each and every spec. Like, for example, cleaning a database.
You can also do the check inside
spec_helper's config.before(:each) block
based on thetest.metadata[:described_class] or [:example_group]
.It has tons of information about the current context so it might be a bit more straightforward to do the filtering from inside the block instead of changing the specs individually.
Whether it's 1 or 10 which don't need it, sadly it doesn't change the fact that any code in spec_helper.rb is going to apply to all. Another option from the answer above is to create a spec_login.rb file with that before hook code, then require it within the test files which need it.
spec/spec_login.rb
spec/1_spec.rb (needs login)
spec/2_spec.rb (no login)