`allow_any_instance_of` 模拟在范围内不起作用
`allow_any_instance_of` mock not working in scope
我的模拟只有在如下所示的 before 块中时才有效。这只是我对我的问题的快速而肮脏的表述。从字面上看,当我将行从 before 块移动到 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
我想要它,但它不起作用:
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
我的错。原来的问题写得不好。访问该页面是调用 #quack
的原因。必须始终在执行任何涉及方法调用的操作之前完成模拟。所以这是我的解决方案
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
我的模拟只有在如下所示的 before 块中时才有效。这只是我对我的问题的快速而肮脏的表述。从字面上看,当我将行从 before 块移动到 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
我想要它,但它不起作用:
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
我的错。原来的问题写得不好。访问该页面是调用 #quack
的原因。必须始终在执行任何涉及方法调用的操作之前完成模拟。所以这是我的解决方案
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