1

我正在做一个相当琐碎的请求规范,但不能让它通过。任何提示将不胜感激:)。

CheckoutController:

def create_membership
  ...
  if @membership.save
    redirect_to checkout_payment_path(id: @membership.id)
  ...
end

def payment
  @membership = Membership.find(params[:id])
  @transaction = Transaction.new
  @transaction.amount = @membership.product.price
  ... 
end

Routes:
  namespace :checkout do
    get 'payment'
  end

RSpec:
  let(:member) { create(:member) }
  let(:membership) { create(:membership, member: member) }
  let(:transaction) { create(:transaction, membership: membership) }

  describe "GET 'payment'" do
    it "return transaction" do
      Membership.any_instance.stub(:find).and_return(membership)
      get :payment, id: membership.id 
      assigns(:transaction).should be_a_new(Transaction)
      ...
    end
  end

我已经设置了工厂,以便为每个创建的成员创建一个关联的产品,因此@membership.product.price是有效的。

rspec 在此行失败get :payment, id: membership.id。错误说undefined method 'price' for nil:NilClass。我确定在正确测试之前我已经设置了实例变量。我在这里犯了什么小错误吗?

干杯

4

1 回答 1

0

从存根中删除 .any_instance:

Membership.stub(:find).and_return(membership)

付款方法是在 Membership 类上进行查找,而不是 Membership 的实例。

于 2013-08-20T18:56:19.803 回答