Intermediate TDD in Rails : Choose a Subscription Plan

Objective


Display the plans in the welcome page to allow the user to choose a subscription plan.

High Level View


  1. When a user is logged in, display the list of all plans
  2. Click on the gold plan
  3. Subscribe the user to the gold plan

Steps


Step 1

Create a file called spec/features/subscription_spec.rb.

require 'rails_helper'
require 'spec_helper'

feature "Subscribe", :type => :feature do
  scenario "to gold plan" do
    visit root_path
    click_link 'Register'
    fill_in 'Email', with: 'bugs@rubyplus.com'
    fill_in 'Password', with: '12345678'

    click_button 'Sign up'
    click_link 'Gold'

    expect(page).to have_text("You are now on gold plan.")
  end

end

Step 2

Run the test. It fails with error: Unable to find link Gold.

Step 3

We need to add the links to welcome page:

<ul>
    <li><%= link_to 'Gold',  %></li>
    <li><%= link_to 'Silver',  %></li>
</ul>

What should be the url_helper we can use?

Step 4

Define the routes in routes.rb:

get 'subscriptions/new' => 'subscriptions#new', as: :new_subscription
post 'subscriptions/create' => 'subscriptions#create'

Step 5

Let's revise view in step 3. When a user clicks on a plan, we will send that as the query parameter so that we know which plan they selected:

<ul>
  <%= link_to 'Gold', new_subscription_path(:plan_name => 'gold') %> <br/>
  <%= link_to 'Silver', new_subscription_path(:plan_name => 'silver') %> <br/>
</ul>

Note that the value for plan_name we have here in the link must match the plan id for the plans you have created in the Stripe dashboard.

Step 6

We need a subscriptions controller.

$rails g controller subscriptions new create

Delete the following Rails generated routes:

  get 'subscriptions/new'

  get 'subscriptions/create'

Step 7

If you run the existing tests generated by rspec. It will pass.

$rspec spec/controllers/subscriptions_controller_spec.rb 

Step 8

Add the new test:

it 'should initialize the plan name' do
  get :new, { plan_name: 'gold' }

  expect(assigns[:plan_name]).to eq('gold')
end

Run the test. It fails.

Step 9

Change the new action in subscriptions_controller.rb:

def new
  @plan_name = params[:plan_name]      
end

Run the test. It passes.

Step 10

The subscriptions controller should use stripe layout.

it 'should use stripe layout' do
  get :new, { plan_name: 'gold' }

  expect(response).to render_template(layout: 'stripe')
end

Run the test.

 1) SubscriptionsController GET #new should use stripe layout
     Failure/Error: expect(response).to render_template(layout: 'stripe')
       expecting layout <stripe> but action rendered <["layouts/application", "application"]>.
       Expected ["layouts/application", "application"] to include "stripe".

Step 11

Create an empty file in app/views/layouts/stripe.html.erb

Step 12

Add the layout 'stripe' in subscriptions controller.

class SubscriptionsController < ApplicationController
  layout 'stripe'  

  def new
    @plan_name = params[:plan_name]      
  end

  def create
  end
end

The test now passes.

Step 13

run the feature spec :

$rspec spec/features/subscription_spec.rb 

It fails with error: Unable to find link Gold.

Step 14

Add the links to the plans in the welcome/index.html.erb.

<h1>My Blog</h1>

<%= link_to 'Gold', new_subscription_path(:plan_name => 'gold') %> <br/>
<%= link_to 'Silver', new_subscription_path(:plan_name => 'silver') %> <br/>

<%= link_to 'My Blog', articles_path %>

Step 15

Run the test. It fails. Copy the same content in application.html.erb to stripe.html for now. Eventually the strip.html.erb will turn off the turbo links when our test calls for it.

Add the text 'You are now on gold plan.' to app/views/subsciptions/new.html.erb. The test passes.

Summary


In this lesson we displayed the plans available for subscription to the user. The feature is not complete, before we can subscribe a user, we need to get their credit card details. We will be discussing it in the upcoming articles.


Related Articles


Create your own user feedback survey