0

这是我的错误

Failure/Error: cart.should_receive(:save!).and_return(true)
       (Mock "Cart_1004").save!(any args)
           expected: 1 time
           received: 0 times

这是我的 rspec 代码:

  describe "#redeem_coupon" do
    let(:coupon) { mock_model Coupon, code: 'specialyug5' }
    let(:cart) { mock_model Cart }
    before { subject.stub(:current_cart) { cart } }

    it "assigns a coupon to the cart" do
      cart.should_receive(:coupon).and_return(coupon)
      post :redeem_coupon, coupon: {coupon: coupon.code}
      assigns(:cart).coupon.should eql(coupon)
    end

    it "redirects to the cart_path" do
      Coupon.any_instance.stub(:find_active_by_code).and_return(coupon)
      cart.should_receive(:save!).and_return(true)
      post :redeem_coupon, coupon: {coupon: coupon.code}
      #response.should redirect_to(cart_path)
    end
  end

(理想情况下,我想测试它是否被重定向,但这显然不是因为整个代码都没有通过)。

## controller
  def redeem_coupon
    @cart = current_cart

    if request.post?
      coupon = Coupon.find_active_by_code(params[:coupon][:coupon], @cart.subtotal, current_member)
      if coupon
        @cart.coupon = coupon
        @cart.save!
      end
    end
    redirect_to cart_path
  end
4

0 回答 0