Reputation: 1168
Hi there
Because i cannot distinguish if i am wrong or if this is a bug i opened a bug report too.
I need to have a describe/context block within a loop and I am facing a totally strange behavior at the point when I want to access a variable from outside the Rspec.describe block.
Its an app with many different user roles that need to be tested. Idea is to have a central variable with all user roles (e.g. [:staff, :company_lead, ...]
) and within the request-specs I want to iterate it.
rspec-core (3.12.2, 3.12.1) rspec-rails (6.1.0, 6.0.1) rails 6.1 ruby 2.7.2
I tested the same on a newer app (ruby 3.2.2, rails 7.0.5, rspec-rails 6.0.1), but same behaviour. But, on examples like GitHub 2010 this seemed to work many years ago.
This works
RSpec.describe '/admin/settings', type: :request do
ALLOWED_ROLES = %i[adm bo_adm]
ALLOWED_ROLES.each do |role|
describe "allowed as #{role}" do
login("user_#{role}")
it "check" do
expect(1).to eq(2)
end
end
end
end
Tests are failing, like expected.
This works too
class Tests
SUBTESTS = [:adm, :bo_adm]
end
RSpec.describe '/admin/settings', type: :request do
Tests::SUBTESTS.each do |v|
context "as_role_" do
it "check" do
expect(1).to eq(2)
end
end
end
end
=> tests are running and failing, but problem: i do not see which subtest is failing because the context label is always the same
This crashes
class Tests
SUBTESTS = [:adm, :bo_adm]
end
RSpec.describe '/admin/settings', type: :request do
Tests::SUBTESTS.each do |v|
context "as_role_#{v}" do
it "check" do
expect(1).to eq(2)
end
end
end
end
At the point where I include the variable v
in the context label, the tests are crashing, it says "No tests were found ... All examples have been filtered out".
I do nothing different than in the first test, which works except dragging the array (Tests::SUBTESTS
) from outside the Rspec.describe
block where the tests reside.
Am I missing something?
Is there another way for doing this?
Currently I have /spec/support/controller_macros.rb
where all the login_as...
methods are defined. There I want to put an array with all user roles and access it from all specs that are inside /spec/requests/...
. But I cannot find a way to make this working.
Thanks, Chris
Upvotes: 0
Views: 58