gpt4 book ai didi

ruby-on-rails - Rails, stripe feature test error - 参数缺失或值为空 : charge

转载 作者:行者123 更新时间:2023-11-28 21:25:56 26 4
gpt4 key购买 nike

我正在尝试为我的 RoR 应用程序编写功能测试,用户必须付费才能提交帖子。用户旅程是;

  1. 用户创建帖子并选择“继续付款”按钮
  2. 然后用户会被带到一个结算页面,他们可以在其中填写“卡号”、“卡验证”和“卡到期”,然后用户按下“支付”按钮。付款由 Stripe 处理。它不是弹出窗口小部件,而是自定义表单。
  3. 如果成功,用户将被重定向到他们的实时帖子

我有一个post模型和一个charge模型。帖子 has_one 收费。 charge belongs_to post。付款是一次性付款,而不是订阅。

我的 Post Controller (仅创建操作):

def create
@post = Post.new(post_params)
@post.user = current_user
@amount = 500
if @post.save
redirect_to new_post_charge_path(@post.id)
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end

我的充电 Controller (仅创建操作):

def create
@charge = Charge.new(charge_params)
@post = Post.find(params[:post_id]);
@charge.post = @post

if @charge.save
Stripe::Charge.create(
:amount => 500,
:currency => "gbp",
:source => params[:charge][:token],
:description => "Wikipost #{@post.id}, #{current_user.email}",
:receipt_email => current_user.email
)
@post.stripe_card_token = @charge.stripe
@post.live = true
@post.save

redirect_to @post, notice: 'Post published successfully'
else
redirect_to new_post_charge_path(@post.id)
end

rescue Stripe::CardError => e
flash[:error] = e.message
return redirect_to new_post_charge_path(@post.id)
end

我正在使用 rspec/capybara 进行测试,并尝试编写如下所示的功能测试,但我不断收到错误消息“缺少参数或值为空:收费”;

require 'rails_helper'

feature 'Publish post' do

before do
@user = create(:user)
end

scenario 'successfully as a registered user', :js => true do
sign_in_as(@user)
click_link 'New post'

expect(current_path).to eq('/posts/new')
fill_in 'post_title', with: 'My new post'
fill_in 'textarea1', with: 'Ipsum lorem.....'

click_button 'Proceed to Payment'

expect(page).to have_content('Billing')

within 'form#new_charge' do
fill_card_details
click_button 'Proceed to Payment'
end

expect(page).to have_content('My new post - published')
end

修复错误或为此用户旅程编写测试的最佳方法是什么?

最佳答案

听起来 Stripe 凭据没有在测试环境中配置。您可能还想研究使用 fake_stripe gem,这样您的测试就不必往返于 Stripe 服务器。

另外 expect(current_path).to eq('/posts/new') 应该写成

expect(page).to have_current_path('/posts/new')

这将允许在检查新路径时使用等待行为,并减少测试的不稳定性。

关于ruby-on-rails - Rails, stripe feature test error - 参数缺失或值为空 : charge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40698116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com