The use of doubles or from rspec-mocks outside of the per-test lifecycle is not supported

40 views
Skip to first unread message

belgoros

unread,
Jul 3, 2018, 9:53:41 AM7/3/18
to rspec
If is not supported, what is the right way to mock an object once before all the examples ?


Here is what I tried to do and failed:

describe '#build_working_hour' do

  context
"for open status" do
      before
(:all) do
       
@schedule = double(MystoreMigration::StoreSchedule, am_start_time: 900, pm_end_time: 2030)
        allow
(@schedule).to receive(:has_divided_hours?).and_return(false)
        allow
(@schedule).to receive(:closed?).and_return(false)
       
@working_hour = store_migrator.build_working_hour(schedule)
     
end


      it
'should init working hour with open status' do
        expect
(@working_hour.open?).to be_truthy
     
end


      it
'should init working hour with open status times' do
        expect
(@working_hour.opens).to eq(32_400_000)
        expect
(@working_hour.closes).to eq(73_800_000)
        expect
(@working_hour.divided_opens).to be nil
        expect
(@working_hour.divided_closes).to be nil
     
end
   
end
end


What is wrong with the above approach ? I'd like to 1) check a status, 2) the assigned values. Thank you!

belgoros

unread,
Jul 3, 2018, 10:00:26 AM7/3/18
to rspec
I figured out after reading the docs:

Mocks are only supported in before(:example).

So the only way to get it working is to use 
before(:example)

  hook.

Hope this helps.

Myron Marston

unread,
Jul 3, 2018, 11:05:08 PM7/3/18
to rs...@googlegroups.com
That works, or you can use `RSpec::Mocks.with_temporary_scope { ... }` to create a scope for use within your `before(:all)` hook.  See this for more information:


--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/88c810fd-bc60-46ba-8ff2-22ef1e16e08a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages