【问题标题】:Stubbing out save methodin rails with rspec使用 rspec 在 rails 中存根保存方法
【发布时间】:2013-01-28 02:19:24
【问题描述】:

我的控制器中有以下操作:

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

但是,存根似乎不起作用,因为保存操作总是成功的。我做错了什么?

谢谢

【问题讨论】:

    标签: ruby-on-rails-3 rspec stub stubbing


    【解决方案1】:

    试试这个:

    Vin.any_instance.stub(:save).and_return(false)
    

    【讨论】:

    • 谢谢。但是,我在该语句中收到未定义的方法“vins”错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 2012-05-18
    • 1970-01-01
    相关资源
    最近更新 更多