ruby on rails - RSpec around() block behaves different from a before() + after() -


i'm using apartment gem manage multi-tenant rails app.

side note: if you're unfamiliar gem, switching apartments switching schema used on postgres db back-end. change apartment tenants apartment::tenant.switch!(apartment)

i have several tests test behavior under context of apartment tenant. that, use below setup (example shown controller specs)

rspec.describe mycontroller, type: :controller   before(:each)     # global before() setup   end    context "foo apartment"      ### option 1 - using around() hook     around(:each) |example|       begin         apartment::tenant.switch!("foo")         example.run       ensure         apartment::tenant.switch!("public")       end     end      ### option 2 - independent before() + after() hooks     before(:each) { apartment::tenant.switch!("foo") }     after(:each) { apartment::tenant.switch!("public") }       "tests foo apartment being used"       expect(apartment::tenant.current).to eq("foo")     end   end end 

as can see, there 2 approaches setting test. 1 uses around() hook , other same thing, independently uses before() , after() hooks.

i'd imagine both approaches equivalent , interchangeable. surprisingly option 2 works.

is there reason behavior? around() block run in different order before() block?

an around(:each) block wrap before(:each) blocks (even defined @ outer scope level). because of option 1 around block you're getting following behavior

around_before   global before() setup block   example runs around_after 

if global setup block sets default tenant or overwriting you're doing in around block

with option 2 get

global before() setup block before block example runs after block 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -