I want to stub a function that returns another stub using Mocha on Minitest. This is the code
@instance = stub('instance') do
stubs(:destroy).returns(true)
stubs(:id).returns(0)
end
find_by_id = stub do
stubs(:find_by_id).returns(nil) #default
stubs(:find_by_id).with(@instance.id).returns(@instance)
end
@user.stubs(:instances).returns(find_by_id)
When I run the code with byebug, @instance is defined:
(byebug) @instance
#<Mock:instance>
however, inside find_by_id = stub do
(byebug) @instance
nil
I read here about using mock instead of stub, but that does not work either.
I don't get why @instance is nil inside the do block, being that it was previously defined.