-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customize context inclusion with let_it_be or AnyFixture #158
Comments
Do I understand correctly: if you replace So, you suggest to make it possible to override describe “smth” do
let_it_be(:user) { create(:user, name: “A”) }
let_it_be(:post) { create(:post, user: user) }
let_it_be(:user) { create(:user, name: “B”) }
specify do
expect(user.name).to eq “B”
expect(post.user).to eq user
expect(User.where(name: “A”).any?).to eq false
end
end |
Yes, that is one way of doing it and I think it is the correct way because this would make I think it is ok if the user "A" exists though, it matches the semantic of The main usage for me would be using it include_context but it boils down to allow overriding describe “smth” do
let_it_be(:user) { create(:user, name: “A”) }
context "with a user named B" do
let_it_be(:user) { create(:user, name: “B”) }
let_it_be(:post) { create(:post, user: user) }
specify do
expect(user.name).to eq “B”
expect(post.user).to eq user
expect(User.where(name: “A”).any?).to eq true # user record exists but is not associated with post
end
end
end Does it make sense ? |
The example you provided should work, The following won't work: describe “smth” do
let_it_be(:user) { create(:user, name: “A”) }
let_it_be(:post) { create(:post, user: user) }
context "with a user named B" do
let_it_be(:user) { create(:user, name: “B”) }
specify do
expect(user.name).to eq “B”
expect(post.user).to eq user
expect(User.where(name: “A”).any?).to eq true # user record exists but is not associated with post
end
end
end |
but it would work with regular the "problem" is that For the moment, you explanation allowed me to make my suite work by introducing new contexts but I think my previous way of using context was more like this (which doe snot work) : describe “smth” do
context "with a user named B" do
# result of include_context("with a post"){ let_it_be(:author) { create(:user, name: “B”) } }
let_it_be(:author) { create(:user, name: “A”) } # this is inside the context file
let_it_be(:post) { create(:post, user: user) } # this is inside the context file
let_it_be(:author) { create(:user, name: “B”) } # this is the customization block
specify do
expect(user.name).to eq “B”
expect(post.author).to eq user
expect(User.where(name: “A”).any?).to eq true # user record exists but is not associated with post
end
end
end |
Overriding Will think about it during the work on the next release. |
With Rspec, there is an idiomatic way to customize context inclusion by providing a block : https://relishapp.com/rspec/rspec-core/docs/example-groups/shared-context#declare-a-shared-context,-include-it-with-%60include-context%60-and-extend-it-with-an-additional-block
This will allow to write thinks like
When following this pattern with
let_it_be
, the customization block is added to the list of before_all, it does not override the previous one.How do you handle this scenario ?
The text was updated successfully, but these errors were encountered: