TDD is not so bad!
Here are a few things to think about getting started:
*You'll need a test suite like RSpec.
These instructions can be made obsolete in the future. Always be sure to check: https://github.com/rspec/rspec-rails
Add this to your Gemfile:
group :development, :test do
gem 'rspec-rails', '~> 3.0'
end
In your terminal: bundle
In your terminal: rails generate rspec:install
In your terminal: bundle binstubs rspec-core
Make sure that you can run in your terminal: rspec
If so, that means you’re in good shape!
*In this example you would need the proper Models in the database which will consist of Product, Order, and CartedProduct Models.
Here is my app on github for your review
OR my spec folder.
*Test your model logic mostly.
*Does any of your logic have validations?
*Find this out on your Mac by clicking command - shift - F and find all "vailidates". This will give you all the items that need validations.
*Add the Simplecov gem to your gem file.
simplecov link Read the documentation carefully! This is really cool and helpful! It will actually tell you, by percentage, how much of your app is covered by tests and where you need tests still.
Ok, let's get started!
Where do you begin?
Let's say you want to test your sub-total method in your orders model.
First you'll need to navigate to your spec folder and add a models folder to it. Then add an order_spec.rb file to the models folder.
At the top of the order_spec file you should place
require 'rails_helper'
Then start your test code block.
Here is an example and then I'll explain:
require 'rails_helper'
describe Order do
describe 'sub_total' do
it 'should return the sub_total of all the carted products prices times the quantity of each' do
product = Product.create!(:price => 3.0, :name => "cone")
product_2 = Product.create!(:price => 5.0, :name => "shake")
order = Order.create!(:user_id => 2, :status => 'cart')
carted_product_1 = CartedProduct.create!(:product_id => product.id, :quantity => 3, :order_id => order.id)
carted_product_2 = CartedProduct.create!(:product_id => product_2.id, :quantity => 2, :order_id => order.id)
expect(order.sub_total).to eq(19.0)
end
end
end
As you notice, first you need to describe what it is you're testing. in this case Order.
describe Order do
Then you need to describe what method you are testing. Again, this will be the sub_total method.
describe 'sub_total' do
Now, you need to tell it what it should do. Notice things in single quotes are arbitrary, they are not part of the code so be as accurate of a description as you can so someone else years from now could understand what you are testing.
it 'should return the sub_total of all the carted products prices times the quantity of each' do
Here's the part you need to think about. You'll need to build everything from scratch because remember that this is a piece of metal. It doesn't know what you want automatically. You have to be specific. You are creating in the test development database a couple of temporary products with the following code because they don't exist yet even though you may have things in you development database.
product = Product.create!(:price => 3.0, :name => "cone")
product_2 = Product.create!(:price => 5.0, :name => "shake")
Notice that I have the price and name? That's because in my app I have validations for these two items.
OK, Next let's create an order.
order = Order.create!(:user_id => 2, :status => 'cart')
Again, I need validations of a user_id and a status of 'cart' to make this happen.
Now, for a couple of carted_products. Think of this like putting you items in the grocery cart at the store before you head to the cashier.
carted_product_1 = CartedProduct.create!(:product_id => product.id, :quantity => 3, :order_id => order.id)
carted_product_2 = CartedProduct.create!(:product_id => product_2.id, :quantity => 2, :order_id => order.id)
Be careful to make your variables different, remember this stupid machine doesn't know anything unless you tell it. If you say something monkey is purple and then on another line say monkey is purple it thinks it's the same item.
Ok, this is where you tell the computer what you are expecting to happen when the RSpec command in the terminal is run.
expect(order.sub_total).to eq(19.0)
This should be what your products above should come to when you run the sub_total method on you order.
Now, go to your terminal and run rspec.
This will generate whether your test passes or fails. If you've installed the simplecov gem properly it will also generate e report for you to see what percentage of your app is properly covered by testing. It will also tell you where you may want tests.
A little confusing to get started but the more you do it the more comfortable you become with running these tests.Good Luck!
Union Operator
CSL Behring