Ruby on rails rspec


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ruby on rails rspec

  1. 1. BDD using RSpec
  2. 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. 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. 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. 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. 6. Writing features● Generally you start describing a class that you want● In terms of what its behavior should be● You have a typical cycle of ● Write a feature ● Make it fail (Red) ● Refactor the code and make it work (green)
  7. 7. describe Book do# all features go hereend
  8. 8. Writing a spec● You start writing specs within an “it” block● Example it “returns a new book object” do b1 = b1.should be_an_instance_of(Book) end● be_an_instance_of is a matcher provided by rspec
  9. 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. 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 => “Learning Rails”, :author=> “Michael Hartman” end
  11. 11. Initializers● Initializing once before all tests before :all do #initialization code goes here end
  12. 12. Defining a test subject :)● You can define a test subject which is basically the object that we are describing subject {}● 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
  13. 13. Various Matchers● should● should_not● be_empty #equivalence check● eq #example actual.should eql(expected)● == #example actual.should == expected
  14. 14. Examples actual.should_not eq(expected) actual.should_not == expectedComparison operators actual.should be > expected actual.should be >= expected actual.should be <= expected actual.should be < expectedType/Class actual.should be_instance_of(expected) actual.should be_kind_of(expected)
  15. 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)