我正在做一个相当琐碎的请求规范,但不能让它通过。任何提示将不胜感激:)。
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
。我确定在正确测试之前我已经设置了实例变量。我在这里犯了什么小错误吗?
干杯