I am trying to write feature tests for my RoR app where users have to pay to submit a post. The user journey is;
- User creates a post and selects the button 'proceed to payment'
- User is then taken to a billing page where they can fill in 'card number' 'card verification' and 'card expiry', user then presses 'pay' button. The payment is processed by Stripe. It is not the pop-up widget, but a custom form.
- If successful, the user is redirected to their live post
I have a post model and a charge model. Post has_one charge. Charge belongs_to post. The payments are one-off payments, not subscriptions.
My Post Controller (create action only):
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
My Charge Controller (create action only):
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
I am testing with rspec/capybara and am trying to write a feature test like below, but I keep getting the error '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
What is the best way to fix the error or write a test for this user journey?