+ All Categories
Home > Technology > Do your test

Do your test

Date post: 15-Jan-2015
Category:
Upload: yura-tolstik
View: 586 times
Download: 4 times
Share this document with a friend
Description:
 
Popular Tags:
20
Yura Tolstik Ruby/Rails developer at Altoros Development twitter: @yltsrc email: [email protected]
Transcript
Page 1: Do your test

Yura TolstikRuby/Rails developer at Altoros Development

twitter: @yltsrcemail: [email protected]

Page 2: Do your test

Do your test

Why?

test beforeor

test after

save timeor

waste time

Page 3: Do your test

Save time writing code???

Spent time == Saved time

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

Page 4: Do your test

Rspec best practices

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

Finished in 0.00116 seconds

Page 5: Do your test

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

  describe '.admins' do  end

  describe '#admin?' do  end

  describe '#name' do  endend

Page 6: Do your test

Establish the context

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

Page 7: Do your test

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

Page 8: Do your test

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

Page 9: Do your test

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

Page 10: Do your test

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

Page 11: Do your test

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

Page 12: Do your test

Cucumber best practices

Page 13: Do your test

Organize your garden

bank_account_add.featurebank_account_delete.featureuser_signup.featureuser_signup_when_invited.featureuser_login.feature

Page 14: Do your test

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

Page 15: Do your test

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

Page 16: Do your 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 "[email protected]" 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

Page 17: Do your test

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 | 

Page 18: Do your test

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

Page 19: Do your test

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


Recommended