我正在做一个相当简单的请求规范,但无法通过。任何提示都会非常感激:)。
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
。我确信在正确测试之前我已经设置了实例变量。我在这里犯了什么小错误吗?
干杯
答案 0 :(得分:0)
从存根中删除.any_instance:
Membership.stub(:find).and_return(membership)
付款方式是在成员资格类上进行查找,而不是成员资格的实例。