Do your test

Post on 15-Jan-2015

586 views 4 download

Tags:

description

 

transcript

Yura TolstikRuby/Rails developer at Altoros Development

twitter: @yltsrcemail: yltsrc@gmail.com

Do your test

Why?

test beforeor

test after

save timeor

waste time

Save time writing code???

Spent time == Saved time

We spend time writing tests, butwe save time with tests, so...

Rspec best practices

$ rake spec...........................................................................................................................................................................................................................................................................................................................................................

Finished in 0.00116 seconds

Describe what you are doingdescribe User do  describe '.authenticate' do  end

  describe '.admins' do  end

  describe '#admin?' do  end

  describe '#name' do  endend

Establish the context

describe '#create' do  context 'given valid credentials' do  end   context 'given invalid credentials' do  endend

it only expects one thingdescribe '#create' do it 'creates a new user' do   User.count.should == @count + 1  end   it 'sets a flash message' do    flash[:notice].should be  end   it "redirects to the new user's profile" do    response.should redirect_to(user_path(assigns(:user)))  endend

Prefer explicitnessdescribe '#new' do context 'when not logged in' do    subject do      response    end

    it 'redirects to the sign in page' do      should redirect_to(sign_in_path)    end

    it 'displays a message to sign in' do      subject.body.should match(/sign in/i)    end  endend

Confirm readabilityUsersController  #create    creates a new user    sets a flash message    redirects to the new user's profile  #show    finds the given user    displays its profile  #show.json    returns the given user as JSON  #destroy    deletes the given user    sets a flash message    redirects to the home page

Use the right matcher

object.should be

7.should respond_to(:zero?).with(0).arguments7.should_not be_zero

expect { model.save! }.to raise_error(ActiveRecord::RecordNotFound)

collection.should have(4).items

Rspec options

rspec spec/*_spec.rb --tag @focus rspec spec/*_spec.rb --tag ~@focusrspec spec/*_spec.rb --tag @speed:slowrspec spec/*_spec.rb --format=progress --color

# spec_helper.rbRSpec.configure do |config| config.color_enabled = true config.formatter = :documentation # :progress, :html, :textmateend

describe "group with tagged specs" do it "example I'm working now", :focus => true do; end it "slow example", :speed => 'slow' do; end it "ordinary example", :skip => true do; endend

Cucumber best practices

Organize your garden

bank_account_add.featurebank_account_delete.featureuser_signup.featureuser_signup_when_invited.featureuser_login.feature

Thinking declaratively

Scenario: Create a slide Given I am signed in as an admin When I go to the admin dashboard And I create a new slide

Scenario: Create a slide Given I am signed as an admin When I go to the admin dashboard And I create a new slide Then I should be able to edit it

Cucumber helps youFeature: Search engine optimization In order to find company As a future customer I want to find company in google

Scenario: Find company in google Given I ask google for "company" Then I should see "http://company.url"

$ cucumber...Given /^I ask google for "([^"]*)"$/ do |arg1| pendingend

#Gemfilegem "cucumber-rails-training-wheels", :group => :test

Make your scenario DRYFeature: A user can cancel a transaction unless it's claimed by the recipient   Background:    Given I am logged in    And I send "$10" to "mukmuk@example.com" from my "Bank account"   Scenario: I can cancel as long as the payment is not claimed    When I cancel my latest transaction    Then I should see a cancellation confirmation   Scenario: I can't cancel once the payment is claimed    Given "Mukmuk" claimed the latest transaction    Then I can't cancel my latest transaction

Scenario with variablesScenario Outline: Add invalid bank account displays inline errors

  Given I follow "Add Bank Account"  When I fill in "<field>" with "<value>"  And I press "Add Bank Account"  And I should see the inline error "<error>" for "<field>"   Examples:    | field   | value         | error                    |    | Account |               | Can't be blank           |    | Account | Sixty five    | Should be 1 to 12 digits |    | Account | 1234567890123 | Should be 1 to 12 digits | 

Cucumber options

cucumber --name "Find site in search engines"

cucumber features --tags @wip:3

cucumber --tags @wip,@smoke # logical OR (@wip || @smoke)

# logical AND (@wip && !@slow)cucumber --tags @wip --tags ~@slow

cucumber features/account_*.feature --format=progress --quiet

@smokeFeature: Find site in search engines @javascript @wip Scenario: Find site in google

Cucumber profiles#cucumber.yml<%std_opts = "--format pretty --quiet --strict --tags ~@wip"%>

default: <%= std_opts << " --tags ~@javascript" %> featuresselenium: <%= std_opts %> featureswip: --tags @wip:3 --wip features

cucumber --profile=wiprake cucumber:wip