Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tested Driven Development With Rspec - Training...

Tested Driven Development With Rspec - Training Session

Avatar for Shaiju Edakulangara

Shaiju Edakulangara

July 24, 2015

More Decks by Shaiju Edakulangara

Other Decks in Technology

Transcript

  1. Take away from this session What is TDD RSpec Basic

    Structure Setting Up Rspec Model Spec Controller Spec SimpleCov
  2. 1 TDD It's 'tests' that 'drive' the 'development' By simply

    running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring
  3. RSpec Rspec is unit-testing framework for Rubyprogramming language Writen in

    Ruby Uses a domain specific language (DSL) Well-formatted output Rspecs strongly recommended with TDD
  4. Shared Examples and Contexts One convention is to put files

    containing shared examples in spec/support/ and require files in that directory from spec/spec_helper.rb: Dir["./spec/support/**/*.rb"].sort.each { |f| require f}
  5. Setting up RSpec group :development, :test do gem "rspec-rails", "~>

    3.1.0" gem "factory_girl_rails", "~> 4.4.1" end Gemfile Test database config/database.yml test: adapter: mysql2 encoding: utf8 database: contacts_test username: root password:
  6. rails generate rspec:install .rspec spec spec/spec_helper.rb spec/rails_helper.rb Setting up RSpec

    RSpec configuration .rspec configurations --color --require spec_helper --format documentation
  7. Setting up RSpec Generators config/application.rb config.generators do |g| g.test_framework :rspec,

    fixtures: true, view_specs: false, helper_specs: false, routing_specs: false, controller_specs: true, request_specs: false g.fixture_replacement :factory_girl, dir: "spec/factories" end
  8. 2 Running spec # Run only model specs bundle exec

    rspec spec/models Use the rspec command to run your specs: bundle exec rspec To run only a subset of these specs: Run only specs for AccountsController bundle exec rspec spec/controllers/accounts_controller_spec.rb Run only a particular example bundle exec rspec spec/controllers/accounts_controller_spec.rb:21
  9. Factories versus fixtures Out of the box, Rails provides a

    means of quickly generating sample data called fixtures. A fixture is essentially a YAML-formatted file which helps create sample data. contacts.yml aaron: firstname: "Aaron" lastname: "Sumner" email: "[email protected]" john: firstname: "John" lastname: "Doe" email: "[email protected]"
  10. Factories versus fixtures factories: Simple, flexible, building blocks for test

    data Factory Girl, an easy-to-use and easy-to-rely-on gem for creating test data without the brittleness of fixtures spec/factories/contacts.rb FactoryGirl.define do factory :contact do firstname "John" lastname "Doe" sequence(:email) { |n| "johndoe#{n}@example.com"} end end it "is invalid without a firstname" do contact = FactoryGirl.build(:contact, firstname: nil) expect(contact.errors[:firstname]).to include("can't be blank") end Example Usage
  11. Model Spec: Sample code require 'rails_helper' describe Contact do it

    "is invalid without a firstname" do contact = FactoryGirl.create(:contact, firstname: nil) expect(contact.errors[:firstname]).to include("can't be blank") end it "is invalid without a lastname" do contact = FactoryGirl.create(:contact, lastname: nil) expect(contact.errors[:lastname]).to include("can't be blank") end it "is invalid without an email address" do contact = FactoryGirl.create(:contact, email: nil) expect(contact.errors[:email]).to include("can't be blank") end it "returns a contact's full name as a string" do contact = FactoryGirl.create(:contact, firstname: "Jane", lastnalastname: "Smith" ) expect(contact.name).to eq 'Jane Smith' end end
  12. require 'rails_helper' describe UsersController do describe 'GET #show' do it

    "assigns the requested contact to @user" do user = FactoryGirl.create(:user) get :show, id: user expect(assigns(:user)).to eq user end it "renders the :show template" do user = FactoryGirl.create(:user) get :show, id: user expect(response).to render_template :show end end end Testing GET requests Basic controller specs spec/controllers/contacts_controller_spec.rb
  13. describe 'GET #index' do it "populates an array of all

    users" do smith = FactoryGirl.create(:user, lastname: 'Smith') jones = FactoryGirl.create(:user, lastname: 'Jones') get :index expect(assigns(:users)).to match_array([smith, jones]) end it "renders the :index template" do get :index expect(response).to render_template :index end end Testing GET requests Basic controller specs spec/controllers/contacts_controller_spec.rb
  14. SimpleCov is a code coverage analysis tool for Ruby SimpleCov

    gem 'simplecov', :require => false, :group => :test Gemfile require 'simplecov' if ENV['RAILS_ENV'] == 'test' require 'simplecov' SimpleCov.start 'rails' puts "required simplecov" end spec_helper.rb RAILS_ENV=test COVERAGE=true bundle exec rspec