Rspec basics
-
Upload
stalin-thangaraj -
Category
Technology
-
view
581 -
download
1
description
Transcript of Rspec basics
![Page 1: Rspec basics](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/1.jpg)
Rspec
Stalin
![Page 2: Rspec basics](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/13.jpg)
Before and After
useful to remove duplicates improving the clarity making the examples easier to understand
![Page 14: Rspec basics](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/18.jpg)
Matchers
Multiline text● result.should match /regexp/● result.should =~ /regexp/
Arrays and hashes● array.should include(2)
![Page 19: Rspec basics](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/22.jpg)
Generated Description
in few cases, example docstring is nearly matches with the expectation in the example
can use Rspec autogenerated example name specify() more readable than it() both are aliases of example()
![Page 23: Rspec basics](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/24.jpg)
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 builtin mock object framework
![Page 25: Rspec basics](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/25.jpg)
Rspec-Mock
Stub● returns predetermined 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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/28.jpg)
Rspec-Rails
gem install rspecrails rails generate rspec:install
![Page 29: Rspec basics](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/29.jpg)
Rspec-Tips
describe your methods● describe ".authenticate" do● describe "#admin?" do
use contexts single expectation
![Page 30: Rspec basics](https://reader033.fdocuments.in/reader033/viewer/2022051314/54b7190b4a7959af718b4672/html5/thumbnails/30.jpg)
References
The Rspec Book by David chelimsky
http://github.com/rspec/rspec http://github.com/rspec/rspecrails