ruby on rails - `allow_any_instance_of` mock not working in scope -
my mock working when it's in before block shown below.  quick , dirty representation of problem.  literally when move line before block does not quack assertion, stops mocking :(
describe 'ducks', type: :feature   before     ...     allow_any_instance_of(duck).to receive(:quack).and_return('bark!')     visit animal_farm_path   end    context 'is odd duck'     'does not quack'       expect(duck.new.quack).to eq('bark!')     end   end end i want here, doesn't work:
describe 'ducks', type: :feature   before     ...     visit animal_farm_path   end    context 'is odd duck'     'does not quack'       allow_any_instance_of(duck).to receive(:quack).and_return('bark!')       expect(duck.new.quack).to eq('bark!')     end   end end 
my bad.  original question poorly written.  visiting page makes #quack call.  mocks must done before whatever engages method call. solution
describe 'ducks', type: :feature   before     ...   end    context 'is odd duck'     'does not quack'       allow_any_instance_of(duck).to receive(:quack).and_return('bark!')       visit animal_farm_path        # in crude example, page prints out animals sound       expect(page).to have_text('bark!')     end   end end 
Comments
Post a Comment