Date post: | 15-Jan-2015 |
Category: |
Technology |
Upload: | stalin-thangaraj |
View: | 576 times |
Download: | 1 times |
Rspec
Stalin
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
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
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
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
In Rspec:
describe "A new Account" do
it "should have 0 balance" do
account = Account.new
account.balance.should == 0.0
end
end
Rspec
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
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
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
Describe it..
describe()
Ex: describe 'A Book' { } # => A Book
describe Book { } # => Book
describe Book, 'without price tag' {} # => Book without price tag
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
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
Before and After
useful to remove duplicates improving the clarity making the examples easier to understand
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
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
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
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)
Matchers
Multiline text● result.should match /regexp/● result.should =~ /regexp/
Arrays and hashes● array.should include(2)
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
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
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
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()
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
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
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")
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
Rspec-Mock
Message expectations● is an expectations● if expected message is not received, example will fail
Ex● Channel.should_receive(:new).with(params)
Rspec-Rails
gem install rspecrails rails generate rspec:install
Rspec-Tips
describe your methods● describe ".authenticate" do● describe "#admin?" do
use contexts single expectation
References
The Rspec Book by David chelimsky
http://github.com/rspec/rspec http://github.com/rspec/rspecrails