【发布时间】:2017-04-03 13:06:39
【问题描述】:
我正在尝试为我的 RoR 应用程序编写功能测试,用户必须付费才能提交帖子。用户旅程是;
- 用户创建帖子并选择“继续付款”按钮
- 然后用户被带到一个账单页面,在那里他们可以填写“卡号”“卡验证”和“卡到期”,然后用户按下“支付”按钮。付款由 Stripe 处理。它不是弹出窗口小部件,而是自定义表单。
- 如果成功,用户将被重定向到他们的实时帖子
我有一个后期模型和一个充电模型。发布 has_one 费用。收费belongs_to 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 进行测试,并尝试编写如下所示的功能测试,但我不断收到错误消息“缺少参数或值为空:收费”;
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
修复错误或为此用户旅程编写测试的最佳方法是什么?
【问题讨论】:
标签: ruby-on-rails testing rspec capybara stripe-payments