Ruby on rails 3 使用rspec在rails中删除save方法
我的控制器中有以下操作:Ruby on rails 3 使用rspec在rails中删除save方法,ruby-on-rails-3,rspec,stub,stubbing,Ruby On Rails 3,Rspec,Stub,Stubbing,我的控制器中有以下操作: 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
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测试。但是,我想取消save操作以模拟失败:
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
但是,存根似乎不起作用,因为保存操作总是成功的。我做错了什么
谢谢试试这个:
Vin.any_instance.stub(:save).and_return(false)
谢谢但是,我在该语句中得到了一个未定义的方法“vins”错误。