2. BDD vs TDD
● Both the approach do emphasize on
testing
● However , in TDD you are basically
testing the functionality of a
function
● As to what should be the output
● In BDD we basically start specifying
features on a top level and then
work our way into a function
3. What is RSpec
● RSpec is testing tool for the
Ruby programming language.
● Born under the banner of
Behaviour-Driven Development, it
is designed to make Test-Driven
Development a productive and
enjoyable experience
4. Rspec features
● a rich command line program (the
rspec command)
● textual descriptions of examples and
groups (rspec-core)
● flexible and customizable reporting
● extensible expectation language
(rspec-expectations)
● built-in mocking/stubbing framework
(rspec-mocks)
5. Installing RSpec
● Once you define rspec features
you will be running the test
suite from command line
● In order to install rspec you
need to install it via gem
● gem install rspec
6. Writing features
● Generally you start describing a
class that you want
● In terms of what it's behavior
should be
● You have a typical cycle of
● Write a feature
● Make it fail (Red)
● Refactor the code and make it work
(green)
8. Writing a spec
● You start writing specs within an
“it” block
● Example
it “returns a new book object” do
b1 = Book.new
b1.should be_an_instance_of(Book)
end
● be_an_instance_of is a matcher
provided by rspec
9. Running tests
● You can run a test from the
terminal
rspec book_spec.rb
● If you want to see more verbose
output on a test then
rspec bowling_spec.rb --format
nested
10. Initializers
● If there is a certain set of
code that you need to initialize
for each tests then you can use
the intializers
● Initializing before each test
before :each do
@book = Book.new(:title=>
“Learning Rails”, :author=>
“Michael Hartman”
end
11. Initializers
● Initializing once before all
tests
before :all do
#initialization code goes here
end
12. Defining a test
subject :)
● You can define a test subject
which is basically the object
that we are describing
subject { Article.new}
● Once defined we can use the
keyword “subject” to use the
matchers on it and test it
e.g.
subject.should be_an_instance_of
Article
14. Examples
actual.should_not eq(expected)
actual.should_not == expected
Comparison operators
actual.should be > expected
actual.should be >= expected
actual.should be <= expected
actual.should be < expected
Type/Class
actual.should
be_instance_of(expected)
actual.should be_kind_of(expected)
15. Boolean check
actual.should be_true # passes if
actual is truthy (not nil or false)
actual.should be_false # passes if
actual is falsy (nil or false)
actual.should be_nil # passes if
actual is nil
actual.should be # passes if
actual is truthy (not nil or false)