Reading Specs: Coupons

Capsule Coding
8 min readOct 7, 2020

--

We open up our test and begin running spec in Terminal

An error occurred while loading ./spec/controllers/coupons_controller_spec.rb.
Failure/Error:
describe CouponsController do

describe "POST create" do
context "with valid attributes" do
it "creates a new coupon" do
expect{
post :create, params: { :coupon => { :coupon_code => "ASD123", :store => "Dean and Deluca" } }
}.to change(Coupon,:count).by(1)
end
NameError:
uninitialized constant CouponsController
# ./spec/controllers/coupons_controller_spec.rb:3:in `<top (required)>'
An error occurred while loading ./spec/models/coupon_spec.rb.
Failure/Error:
describe Coupon do
before(:each) do
@coupon = Coupon.create!(coupon_code: "FREESTUFF", store: "Chipotle")
end

it 'can be created' do
expect(@coupon).to be_valid
end
end
NameError:
uninitialized constant Coupon
# ./spec/models/coupon_spec.rb:3:in `<top (required)>'
Finished in 0.03563 seconds (files took 4.08 seconds to load)
0 examples, 0 failures, 2 errors occurred outside of examples

We take the first part of our failure:

Failure/Error:
describe CouponsController do

describe "POST create" do
context "with valid attributes" do
it "creates a new coupon" do
expect{
post :create, params: { :coupon => { :coupon_code => "ASD123", :store => "Dean and Deluca" } }
}.to change(Coupon,:count).by(1)
end
NameError:
uninitialized constant CouponsController

Which tells us that we must create a controller!

Created Coupons Controller solved our first failure.

Above is the created CouponController, and then we run learn again in Terminal and get the next failure below.

Next our test is asking us to create a model for Coupon.

Once created we pass our test, it opens up many more test for us to complete.

Let’s break these test down in order that it is given to us.

Failures:1) CouponsController POST create with valid attributes creates a new coupon
Failure/Error:
expect{
post :create, params: { :coupon => { :coupon_code => "ASD123", :store => "Dean and Deluca" } }
}.to change(Coupon,:count).by(1)

ActiveRecord::StatementInvalid:
Could not find table 'coupons'

Our failure now reads that it could not find the table ‘coupons’

To make our Coupon table:

  1. Create a Migrate directory inside the DB directory.
  2. Create a file (001_create_coupons.rb)
  3. Fill with attributes for coupon.

Once you have a file created and our attributes for the object it should look as so below. We use t.strings for both of our attributes and add a null:false so if left empty it will return false.

We run “rails db:migrate” which creates our schema.

After this is done we run our test again and check to see if our failure is passed.

We pass our first test but we notice that it wasn’t the first one. the test we passed was:

Well lets continue on:

Below I added all my failures and I notice that we will also have a create action, a new action, and an index action all inside of our Coupons Controller. Therefore I will go ahead and add them into my Coupons Controller.

1) CouponsController POST create with valid attributes creates a new coupon
Failure/Error: post :create, params: { :coupon => { :coupon_code => "ASD123", :store => "Dean and Deluca" } }

AbstractController::ActionNotFound:
The action 'create' could not be found for CouponsController
2) CouponsController POST create with valid attributes redirects to the new coupon
Failure/Error: post :create, params: { :coupon => { :coupon_code => "ASD123", :store => "Dean and Deluca" } }

AbstractController::ActionNotFound:
The action 'create' could not be found for CouponsController
3) Route to view has an index page
Failure/Error: visit coupons_path

AbstractController::ActionNotFound:
The action 'index' could not be found for CouponsController
4) Multiple coupons are shown on the index page
Failure/Error: visit coupons_path

AbstractController::ActionNotFound:
The action 'index' could not be found for CouponsController
5) form page form renders with the new action
Failure/Error: visit new_coupon_path

AbstractController::ActionNotFound:
The action 'new' could not be found for CouponsController
6) form page new form submits content and renders form content
Failure/Error: visit new_coupon_path

AbstractController::ActionNotFound:
The action 'new' could not be found for CouponsController
7) form page creates a record in the database
Failure/Error: visit new_coupon_path

AbstractController::ActionNotFound:
The action 'new' could not be found for CouponsController
8) Show page renders properly
Failure/Error: visit coupon_path(@coupon)

AbstractController::ActionNotFound:
The action 'show' could not be found for CouponsController
9) Show page renders the coupon code in a h1 tag
Failure/Error: visit coupon_path(@coupon)

AbstractController::ActionNotFound:
The action 'show' could not be found for CouponsController
10) Show page renders the store name in a h1 tag
Failure/Error: visit coupon_path(@coupon)

AbstractController::ActionNotFound:
The action 'show' could not be found for CouponsController
11) linking from the index page to the show page index page coupon code text is hyperlinked to coupon page
Failure/Error: visit coupons_path

AbstractController::ActionNotFound:
The action 'index' could not be found for CouponsController
Finished in 0.07612 seconds (files took 1.75 seconds to load)
12 examples, 11 failures

After creating our controller actions we also add a route for our coupons.

and check our failures again.

Failures:1) CouponsController POST create with valid attributes creates a new coupon
Failure/Error:
expect{
post :create, params: { :coupon => { :coupon_code => "ASD123", :store => "Dean and Deluca" } }
}.to change(Coupon,:count).by(1)

expected `Coupon.count` to have changed by 1, but was changed by 0
# ./spec/controllers/coupons_controller_spec.rb:8:in `block (4 levels) in <top (required)>'
2) CouponsController POST create with valid attributes redirects to the new coupon
Failure/Error: expect(response).to redirect_to Coupon.last
Expected response to be a <3XX: redirect>, but was a <204: No Content>
# ./spec/controllers/coupons_controller_spec.rb:15:in `block (4 levels) in <top (required)>'
3) Route to view has an index page
Failure/Error: visit coupons_path

ActionController::UnknownFormat:
CouponsController#index is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
4) Multiple coupons are shown on the index page
Failure/Error: visit coupons_path

ActionController::UnknownFormat:
CouponsController#index is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
5) form page form renders with the new action
Failure/Error: visit new_coupon_path

ActionController::UnknownFormat:
CouponsController#new is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
6) form page new form submits content and renders form content
Failure/Error: visit new_coupon_path

ActionController::UnknownFormat:
CouponsController#new is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
7) form page creates a record in the database
Failure/Error: visit new_coupon_path

ActionController::UnknownFormat:
CouponsController#new is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
8) Show page renders properly
Failure/Error: visit coupon_path(@coupon)

AbstractController::ActionNotFound:
The action 'show' could not be found for CouponsController
9) Show page renders the coupon code in a h1 tag
Failure/Error: visit coupon_path(@coupon)

AbstractController::ActionNotFound:
The action 'show' could not be found for CouponsController
10) Show page renders the store name in a h1 tag
Failure/Error: visit coupon_path(@coupon)

AbstractController::ActionNotFound:
The action 'show' could not be found for CouponsController
11) linking from the index page to the show page index page coupon code text is hyperlinked to coupon page
Failure/Error: visit coupons_path

ActionController::UnknownFormat:
CouponsController#index is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
Finished in 0.11627 seconds (files took 1.58 seconds to load)
12 examples, 11 failures

Lets check our failures once more.

Perfect now to fill out our create method then run our test.

We pass 2 more test.

9 test left, and ill leave you to it.

d

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Capsule Coding
Capsule Coding

No responses yet

Write a response