我正在尝试为我的RoR应用编写功能测试,用户必须付费才能提交帖子。用户旅程是;
我有一个帖子模型和一个收费模型。邮寄已收费。费用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进行测试,并尝试编写如下所示的功能测试,但我不断收到错误' param丢失或值为空: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
修复错误或为此用户旅程编写测试的最佳方法是什么?
答案 0 :(得分:1)
听起来好像测试环境中没有配置Stripe凭据。您可能还想查看使用fake_stripe gem,以便您的测试不必往返条带服务器。
此外expect(current_path).to eq('/posts/new')
应写为
expect(page).to have_current_path('/posts/new')
允许在检查新路径时使用等待行为并减少测试剥落。