我在 Controller 中执行以下操作:
def create
@user = current_user
@vin = @user.vins.new(params[:vin])
if @vin.save
# waiting for implementation
logger.debug("here we are")
else
redirect_to(vins_path)
end
end
我想用rspec进行测试。但是,我想存盘保存操作以模拟失败:
it "should send user to vins_path if there is a problem creating the VIN" do
@vin.stub!(:save).and_return(false)
post 'create', :vin => { :name => "Test 1", :vin => "test" }
response.should redirect_to(vins_path)
end
但是,该 stub 似乎不起作用,因为保存操作总是成功的。我究竟做错了什么?
谢谢
最佳答案
试试这个:
Vin.any_instance.stub(:save).and_return(false)
关于ruby-on-rails-3 - 使用rspec在Rails中 stub 保存方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14554573/