rspec basics

30
Rspec Stalin

Upload: stalin-thangaraj

Post on 15-Jan-2015

589 views

Category:

Technology


1 download

DESCRIPTION

Small introduction about Rspec and explains the basics in it.

TRANSCRIPT

Page 1: Rspec basics

Rspec

Stalin

Page 2: Rspec basics

About testing

What programmers think...  I'm not going to write all those tests  It's really simple code, it doesn't need to be tested Testing is a waste of time I've done this (loop/data retrieval/functionalty, etc) 

millions of times

Page 3: Rspec basics

About testing

What Project managers may  think... We test after the code is done That's what we have a testing person for We can't spend that time now

Page 4: Rspec basics

What's it about?

Writing specifications of what we are going to do It means we specify the behaviour of our code Specifying a small aspects in a concise, 

unambiguous, and executable form

Page 5: Rspec basics

Domain Specific Language for describing the expected behaviour of a system with executable examples.

 you: Describe a new account

 client: It should have zero balance

Rspec

Page 6: Rspec basics

In Rspec:

       describe "A new Account" do

         it "should have 0 balance" do

            account = Account.new

            account.balance.should == 0.0

          end

        end

Rspec

Page 7: Rspec basics

expectationAn expression of how an object is expected to behave

code exampleAn executable example of how the code can be used, and its expected behaviour (expressed with expectations) in a given context

example group A group of code examples

spec or spec file A file contains one or more example groups

Rspec

Page 8: Rspec basics

Test::Unit Rspec

class DogTest describe Dog

def test_bark It 'should bark'

assert_xxx object.should_be xxx

def setup before(:each) {}

def teardown dfter(:each){}

Rspec

Page 9: Rspec basics

Describe it..

describe() example group receives any number of arguments and a block generally  two arguments that represent the object and 

its behaviour first argument can be a class or module or a string second one is optional but it must be a string returns a sub class of Spec::Example::ExampleGroup

Page 10: Rspec basics

Describe it..

describe()

Ex: describe 'A Book' { } # => A Book

describe Book { } # => Book

describe Book, 'without price tag' {} # => Book without price tag

Page 11: Rspec basics

Describe it..it()

code example receives string as first argument, option hash and 

optional block string can be a sentence that will represent the detail 

of the code within the block pending()

● keep list of specifications to be verified● it() method with no block● Calling pending() midst of the code example

Page 12: Rspec basics

Before and After

before● grouping things by initial state●  before(:each){} executed before executing every 

example● before(:all){} executed before executing all the 

examples in the example group

after●  after(:each){} executed after executing every example●  after(:all){} executing after executing all the examples 

in the example group

Page 13: Rspec basics

Before and After

useful to remove duplicates improving the clarity making the examples easier to understand

Page 14: Rspec basics

Helper methods

another way to have our examples clean can move same line of code to helper methods fails in readability, go somewhere to refer the method shared helper methods used to share helper methods 

across example groups by defining in a module and including it

Page 15: Rspec basics

Nested example groups

 a way to organize example groups in a spec

 describe "outer" do

     describe "inner" {}

  end outer group is a sub class of ExampleGroup inner group is the sub class of outer group everything declared in outer group will be available to 

inner group

Page 16: Rspec basics

Expectations

 expression of how the object is expected to behave describing an expectation of what should happen 

rather than what will happen should & should_not

Page 17: Rspec basics

Matchers

 Equality● a.should == b● a.should === b● a.should eql b● a.should equal b

Floating point●  result.should be_within(5.25).of(0.005)

Page 18: Rspec basics

Matchers

Multi­line text● result.should match /regexp/● result.should =~ /regexp/

Arrays and hashes● array.should include(2)

Page 19: Rspec basics

Matchers

Changes● lambda{}.should change{}● make more explicit by to(), by() and from()

Expecting errors● lambda{}.should raise_error()● raise_error will receive 0,1 or 2 arguments

Page 20: Rspec basics

Matchers

Predicate Matchers●  a method name ends with '?' and returns boolean is 

called predicate matchers●  array.empty?.should == true # here predicate method is expected 

to return true

●  array.should be_empty●   will work for any predicate method, with argument 

also

Page 21: Rspec basics

Matchers

 Have● has_[predicate method] => have_[name]● hash.has_key?(id).should == true  ● hash.should have_key(id)● when describing object itself a collection

–    team.should have(11).players_on_the_field● Strings

–  "this string".should have(11).characters

Page 22: Rspec basics

Generated Description

in few cases, example docstring is nearly matches with the expectation in the example

can use Rspec auto­generated example name specify() more readable than it() both are aliases of example()

Page 23: Rspec basics

Subjectivity

subject of an example is the object being described subject is an instance of the class which is specified in 

example group it is initiated in the before block

Page 24: Rspec basics

Rspec-Mock

mock objects are imitation objects gives control over their behaviour during the 

execution of an example isolates our examples from services that are

complex to set up or expensive to run Rspec comes with a built­in mock object framework

Page 25: Rspec basics

Rspec-Mock

Stub● returns pre­determined value

Ex● book.stub(:title) { "The RSpec Book" }● book.stub(:title => "The RSpec Book")● book.stub(:title).and_return("The RSpec Book")

Page 26: Rspec basics

Rspec-Mock

Consecutive return values● more than one value can be provided to and_return()● Last value will be return for subsequent matches

Ex● die.stub(:roll).and_return(1,2,3)● die.roll # => 1● die.roll # => 2● die.roll # => 3● die.roll # => 3

Page 27: Rspec basics

Rspec-Mock

Message expectations● is an expectations● if expected message is not received, example will fail

Ex● Channel.should_receive(:new).with(params)

Page 28: Rspec basics

Rspec-Rails

gem install rspec­rails rails generate rspec:install

Page 29: Rspec basics

Rspec-Tips

describe your methods● describe ".authenticate" do● describe "#admin?" do

use contexts single expectation

Page 30: Rspec basics

References

The Rspec Book by David chelimsky

http://github.com/rspec/rspec http://github.com/rspec/rspec­rails