0
我有一個控制器操作的東西,如:如何用Rspec模擬模型屬性?
@widget = Widget.new(permitted_params)
@widget.user_id = current_user.id
if @widget.save
@widget
else
{ errors: @widget.errors.full_messages }
end
而且我試圖創建該控制器的規格。
widget = mock_model(Widget)
allow(Widget).to receive(:new).and_return(widget)
allow(widget).to receive(:user_id).and_return(widget)
allow(widget).to receive(:save).and_return(true)
expect(widgets).to receive(:build)
expect(widget).to receive(:save)
post '/v2/widgets', name: 'foo'
expect(json_response).to eq widget.as_json
現在奇怪的事情,我得到:
Failure/Error: post '/v2/widgets', name: 'foo'
#<Double "Widget_1133"> received unexpected message :user_id= with (1129)
即使我有
allow(widget).to receive(:user_id).and_return(widget)
任何幫助是什麼錯誤我在幹什麼?
謝謝