`allow_any_instance_of` mock does not work in scope

My layout only works when it is in the previous block shown below. This is just my quick and dirty idea of ​​my problem. Literally, when I move a line from the previous block to the statement does not quack , it stops taunting does not quack

 describe 'Ducks', type: :feature do before do ... allow_any_instance_of(Duck).to receive(:quack).and_return('bark!') visit animal_farm_path end context 'is an odd duck' it 'does not quack' do expect(Duck.new.quack).to eq('bark!') end end end 

I want it here, but it does not work:

 describe 'Ducks', type: :feature do before do ... visit animal_farm_path end context 'is an odd duck' it 'does not quack' do allow_any_instance_of(Duck).to receive(:quack).and_return('bark!') expect(Duck.new.quack).to eq('bark!') end end end 
+5
source share
1 answer

My bad. The initial question was poorly written. Visiting the page calls #quack . Before doing what is required to call a method, you should always do bullying. So that was my decision

 describe 'Ducks', type: :feature do before do ... end context 'is an odd duck' it 'does not quack' do allow_any_instance_of(Duck).to receive(:quack).and_return('bark!') visit animal_farm_path # In this crude example, the page prints out the animals sound expect(page).to have_text('bark!') end end end 
+7
source

All Articles