Rails,条带特征测试错误 - 参数缺失或值为空:charge

Rails, stripe feature test error - param is missing or the value is empty: charge

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

  1. 用户创建 post 并选择按钮 'proceed to payment'
  2. 然后用户被带到一个计费页面,他们可以在其中填写 'card number' 'card verification' 和 'card expiry',然后用户按下 'pay' 按钮。付款由 Stripe 处理。它不是弹出窗口小部件,而是自定义表单。
  3. 如果成功,用户将被重定向到他们的直播 post

我有一个post模型和一个收费模型。 Posthas_one充电。充电belongs_topost。付款是一次性付款,而不是订阅。

我的 Post 控制器(仅创建操作):

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

我的充电控制器(仅限创建操作):

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 进行测试,并尝试编写如下所示的功能测试,但我一直收到错误 'param is missing or the value is empty: charge';

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,这样您的测试就不必往返于条纹服务器。

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

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

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