How can I define a method create_all
that allows me instead of writing
describe "some spec" do
let(:a) { create :a }
let(:b) { create :b, :a => a }
...
to write
describe "some spec" do
create_all
...
More specifically: Where do I have to define it in order to be able to use it in the describe
context?
It should work across different spec files.
There is a mechanism in RSpec to do this, and it is shared_context
. It is simple, elegant and doesn't require jumping through hoops as some of the other options need you to do.
So, in your example you'd set up a shared context:
# spec/support/create_all.rb
shared_context "create all" do
let(:a) { create :a }
let(:b) { create :b, :a => a }
...
end
Then in your specs
# some_spec.rb
describe "some spec" do
include_context "create all"
it "tests something" do
...
end
end
Some further reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With