Rspec basics

1,150 views

Published on

Small introduction about Rspec and explains the basics in it.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,150
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rspec basics

  1. 1. Rspec Stalin
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. In Rspec:        describe "A new Account" do          it "should have 0 balance" do             account = Account.new             account.balance.should == 0.0           end         end Rspec
  7. 7. expectation An expression of how an object is expected to behave code example An 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
  8. 8. 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
  9. 9. 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
  10. 10. Describe it.. describe() Ex:  describe 'A Book' { } # => A Book  describe Book { } # => Book  describe Book, 'without price tag' {} # => Book without  price tag
  11. 11. 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
  12. 12. 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
  13. 13. Before and After  useful to remove duplicates  improving the clarity  making the examples easier to understand
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. 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)
  18. 18. Matchers  Multi­line text ● result.should match /regexp/ ● result.should =~ /regexp/  Arrays and hashes ● array.should include(2)
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. 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()
  23. 23. 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
  24. 24. 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
  25. 25. 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")
  26. 26. 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
  27. 27. Rspec-Mock  Message expectations ● is an expectations ● if expected message is not received, example will fail  Ex ● Channel.should_receive(:new).with(params)
  28. 28. Rspec-Rails  gem install rspec­rails  rails generate rspec:install
  29. 29. Rspec-Tips  describe your methods ● describe ".authenticate" do ● describe "#admin?" do  use contexts  single expectation
  30. 30. References  The Rspec Book by David chelimsky  http://github.com/rspec/rspec  http://github.com/rspec/rspec­rails

×