Skip to content

Instantly share code, notes, and snippets.

@bitfidget
Last active August 29, 2015 14:00
Show Gist options
  • Save bitfidget/2d8d6aabbdc2ccf22e1b to your computer and use it in GitHub Desktop.
Save bitfidget/2d8d6aabbdc2ccf22e1b to your computer and use it in GitHub Desktop.

rspec testing rails

gems:

group :test, :development do gem 'rspec-rails' gem 'simplecov' gem 'factory_girl_rails' gem 'faker' gem 'shoulda-matchers' end

(make sure these are in the dev gorup or the generators wont work properly)

The shoulda gem is for testing database relations.

bundle these. Then run the rspec gem install command (this creates the rspec folder structure: rails generate rspec:install)

make sure you have a TEST database setup in your database.yml file AND you have postgresql switched ON!

everytome you run rake migrate, you also need to create a clone copy for testing:

rake db:migrate rake db:test:clone

Everytime you create a model (rails generate model Stair) you also need to setup an rspec file for the tests (in this case: touch spec/models/stair_spec.rb).

There should be a test for every property that your model 'should' have.

Whenever you need to test, in terminal just type 'rspec'.

Write your test first, make sure it fails, then do the work to your models and code to make the test pass.

to avoid having to double-up on your db:migrate and db:test:clone commands

add the following to your rake file:

namespace :db do desc "Migrates development and test databases" task :migrate do puts "Migrating development database" Rake::Task["db:migrate"].invoke

puts "Migrating test database" Rake::Task["db:test:clone"].invoke puts "Test migration complete" end end

this will now create the test db every time you run rake db:migrate

simplecov

The simplecov gem will show you how much of your code you are actually testing (you should be testing 100% of your code!). Use this to identify areas where you need to create more tests.

Read this for some good ways of automating the process: http://www.railstutorial.org/

hint

in your rspec file (in root of your app directory) add -f d (for nicer formatting) or -f p (for dot formatting when yo have LOTS of tests to run)

factory girl

include gem: gem 'factory_girl_rails' include gem: gem 'faker'

bundle your gems.

in your spec folder add a new file called factories.rb <- if you ahve the gem isntalled when you reate the model you wont need to do this as FG will create it's own files per model

and include something like:

FactoryGirl.define do factory :post do |f| f.sequence(:title) { |n| "TDD Post #{n}"} content { Faker::Lorem.sentence } end end

Now, inside your specs, where you need a (post) created to test with you can now use @post = FactoryGirl.create :post. You can override some of the factorygirl definition when you call it also, eg. @post = FactoryGirl.create :post, :title => nil You can also use @post = FactoryGirl.build to create a new (post) but not yet save it to the DB (this is the same as Post.new).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment