Reputation: 1621
I am using RSpec.shared_context
to set variables that all the describe blocks will use.
Something like this
RSpec.shared_context "common" do
let(:name) { #creates a database object }
#more let statements
end
Now I invoke it from describe block like so
describe "common test" do
include_context "common"
#run few tests
end
Now after running the describe block I want to clean it up. How do I rollback all the objects created in the shared context?
I tried cleaning it in the after(:context)
hook but since it is a let statement the variable name
is only allowed inside examples.
Is there someway I can use use_transactional_fixtures
to clean this up after running the tests in the describe block.
Upvotes: 0
Views: 563
Reputation: 102036
You don't need to worry about cleaning up your "lets" if you just setup your test suite properly to wipe the database.
Use let to define a memoized helper method. The value will be cached across multiple calls in the same example but not across examples.
Note that let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked.
In almost every case you want teardown to happen automatically and per example. Thats what config.transactional_fixtures
does - it rolls back the database after every example so that you have a fresh slate and don't get test ordering issues. Relying on each example / context whatever to explicitly clean up after itself is just a recipe for failure.
Upvotes: 1