Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 434 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
before(:each) for all tests except one

#1
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`?
Reply

#2
You can also do the check inside `spec_helper's config.before(:each) block`
based on the `test.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.
Reply

#3
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.

# foo_spec.rb
describe Foo do
describe "#bar" do
it "needs to log in" do
expect(1).to eq 1
end
end
describe "#baz" do
it "needs to not log in", :logged_out do
expect(1).to eq 1
end
end
end

So we added metadata to our `it` block. Next, we configure the `before` hook to evaluate the example's metadata.

config.before(:each) do |test|
login(email, password) unless test.metadata[:logged_out]
visit root_url
end

Now, each test will `visit root_url` but only the ones not tagged with `:logged_out` will call `login`.

RSpec calls these metadata based hooks filters. You can learn a bit more about them [here][1].


[1]:

[To see links please register here]

Reply

#4
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*

RSpec.configure do |config|

config.before(:each) do
login(email, password)
visit root_url
end

end


*spec/1_spec.rb (needs login)*

require 'spec_login'

describe 'BlahBlah' do

end


*spec/2_spec.rb (no login)*

describe 'BlahBlah' do

end
Reply

#5
Putting 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.

RSpec.describe 'something' do
context 'specs with login' do
before do
login(email, password)
visit root_url
end

it { ... }
end

context 'specs without login' do
it { ... }
end
end

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.

config.before :each do
DatabaseCleaner.clean
end

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through