avatarAlex Duterte

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1156

Abstract

, Cart, and Item.</p><figure id="0e55"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*D0ahPYzlbvpwxgGcx6HIhQ.png"><figcaption></figcaption></figure><p id="7ab7">When we use:</p><div id="ff5d"><pre>rails g resource Model <span class="hljs-keyword">attribute</span>:<span class="hljs-keyword">type</span></pre></div><p id="21df">The rspec file will be created for that model. To manually generate the file we can use</p><div id="2c1f"><pre>rails g rspec:<span class="hljs-keyword">model</span> model_name</pre></div><h1 id="cad0">Creating a test</h1><p id="7a9e">Lets create a test that totals all items in a users cart. Inside the user_spec.rb</p><figure id="7eb5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*3GXxuuuWoG_PJ_icvAkzrQ.png"><figcaption></figcaption></figure><p id="ed97">Inside the #setup we are creating test cases. Inside the #verify we have:</p><div id="53e5"><pre>expect(user.total_cart).to e<span class="hljs-string">q(5.00)</span></pre></div><h1 id="8eee">Coding to the Test Specs</h1><p id="4c18">When we run rspec we get the following error:</p><figure id="d0ac"><img src="https://cdn-images-1.r

Options

eadmedium.com/v2/resize:fit:800/1*IiAO3Q8milXGXYpHjepV9w.png"><figcaption></figcaption></figure><p id="63be">We can see NoMethodError for total_cart. So lets create a method for that in the User model.</p><figure id="e2ed"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*q3r3BI6kLCZZ0BA4HP1ylg.png"><figcaption></figcaption></figure><p id="a5f8">We got rid of no method error, but we expected 5.0</p><p id="a475">So let’s see if can code something to pass this test spec.</p><figure id="3858"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Kaqv8YSujd_cljYsyk5N3g.png"><figcaption></figcaption></figure><p id="3a45">After coding out the method and running rspec, we can see we passed the test!</p><h2 id="4f8f">Why TDD?</h2><p id="73a4">In the above example, we used a very easy test scenario. But what if it was more complex? What if we wanted to refactor? We can always test our code against the expected outcome. We can also zero in to where our code breaks.</p><p id="71e5">For more documentation on Rspec-rails check out the docs <a href="https://relishapp.com/rspec/rspec-rails/docs">here</a>.</p></article></body>

Test Driven Development in Ruby on Rails using Rspec

Test Driven Development (aka TDD) is where you create test specs of expected outcomes. You can then code your way to pass these tests. The opposite of this method of coding is Behavioral Driven Development (BDD).

Setting up

First thing we need to do is install the ‘rspec-rails’ gem. We want this in the development test section in our gem file.

Don’t forget to bundle install!

We can then create our rspec files using

rails g rspec:install

For this example let’s pretend we have 3 models. User, Cart, and Item.

When we use:

rails g resource Model attribute:type

The rspec file will be created for that model. To manually generate the file we can use

rails g rspec:model model_name

Creating a test

Lets create a test that totals all items in a users cart. Inside the user_spec.rb

Inside the #setup we are creating test cases. Inside the #verify we have:

expect(user.total_cart).to eq(5.00)

Coding to the Test Specs

When we run rspec we get the following error:

We can see NoMethodError for total_cart. So lets create a method for that in the User model.

We got rid of no method error, but we expected 5.0

So let’s see if can code something to pass this test spec.

After coding out the method and running rspec, we can see we passed the test!

Why TDD?

In the above example, we used a very easy test scenario. But what if it was more complex? What if we wanted to refactor? We can always test our code against the expected outcome. We can also zero in to where our code breaks.

For more documentation on Rspec-rails check out the docs here.

Ruby on Rails
Programming
Coding
Test Driven Development
Rspec
Recommended from ReadMedium