Capsule Coding
3 min readSep 30, 2020

LEARN TO CODE+READ SPEC

That takes care of the first part of our error.

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

To break down this line of code..

post :create, params: { :coupon => { :coupon_code => "ASD123", :store => "Dean and Deluca" } }
}.to change(Coupon,:count).by(1)

post = is the request

(:create,) = to create after the request we created

with a params: of { :coupon => { :coupon_code => “ASD123”, :store => “Dean and Deluca” } }

.to change = should change

(Coupon,:count).by(1) the Coupon’s(Coupon Class) Count Method(:count method in our Coupon model) by 1(.by(1)

First Error We run into is “An error occurred while loading ./spec/controllers/coupons_controller_spec.rb.”

and if we check out controller directory we have no Coupon Controller. So we are missing our controller.

To resolve this we simply create a controller using,“rails generate/g controller coupon” or manually with touch app/coupon_controller.rb

“rails generate/g controller coupon”

Now running “rails g controller coupon” also created other files that we don’t need for this test.

rails g controller coupon
rails g controller coupon

So in order to generate only the files needed for this test lets use

rails g controller coupons --no-test-framework --no-helper --no-assets
rails g controller coupons — no-test-framework — no-helper — no-assets

And just for the knowledge we can also use “rails g resource coupons — no-test-framework — no-helper — no-assets” which will create our routes.

rails g resource coupons — no-test-framework — no-helper — no-assets
rails g resource coupons — no-test-framework — no-helper — no-assets
Everything created by rails g resource coupons — no-test-framework — no-helper — no-assets

WE RUN OUR TEST AND Receive a New Error!!!

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