簡體   English   中英

如何測試控制器中的RSpec關聯已設置?

[英]How do I test RSpec association in controller was set?

我有以下幾點:

let(:coupon) { mock_model Coupon, code: 'special' }
let(:cart) { mock_model Cart }

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

我有以下錯誤:

 Failure/Error: assigns(:cart).coupon.should eql(coupon)
 NoMethodError:
   undefined method `coupon' for nil:NilClass

我只想確保以下工作有效:

@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

我猜想current_cart返回nil。 您可以使用已經創建的模擬購物車將其存根:

before { subject.stub(:current_cart) { cart } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM