我被一个测试场景卡住了。
我有一个控制器方法:
def update
@object = Object.find params[:id]
# some other stuff
@object.save
rescue ActiveRecord::StaleObjectError
# woo other stuff
end
字符串
我测试的第一部分是:
context '#update'
let(:object) { double }
it 'nothing fails' do
# some other stuff
expect(Object).to receive(:find).with('5').and_return(object)
expect(object).to receive(:save).and_return(true)
xhr :put, :update, id:5
expect(response).to be_success
expect(assigns(:object)).to eq(object)
end
end
型
现在我想测试ActiveRecord::StaleObjectError
异常。我想把它存根,但我没有找到任何解决方案如何做到这一点。
所以我的问题是,如何在RSpec测试中提升ActiveRecord::StaleObjectError
?
2条答案
按热度按时间n3ipq98p1#
像这样,比如说
字符串
cigdeys32#
我会这样做:
字符串
请注意,我对测试设置中的stubbing方法(在本例中我更喜欢
allow
)和实际期望值(expect
)进行了区分。