Published on

A quick overview on TDD, BDD and RSpec.

Published in: Technology, Spiritual
  • Be the first to comment

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

No notes for slide
  • TDD, BDD, RSpec

    1. 1. Presented by Nazia Hossain Nascenia IT
    2. 2. Testing in Software Engineering <ul><li>Testing is to determine if the requirements of the application are met </li></ul><ul><li>Classification: </li></ul><ul><ul><li>White box testing </li></ul></ul><ul><ul><li>Black box testing </li></ul></ul>
    3. 3. Classification <ul><li>White Box Testing: </li></ul><ul><li>- Tests specific paths through the code </li></ul><ul><li>- Tests decision points </li></ul><ul><li>Black Box Testing: </li></ul><ul><li>- Treats the program like black-box </li></ul><ul><li>- Tests specific input & output </li></ul>
    4. 4. TDD (Test-Driven Development) <ul><li>Repetition of very short development cycle </li></ul><ul><li>Test-first programming concept </li></ul><ul><ul><li>Write some code </li></ul></ul><ul><ul><li>Run the automated tests and see them succeed </li></ul></ul><ul><ul><li>Refactor code </li></ul></ul><ul><ul><li>Repeat </li></ul></ul>
    5. 5. BDD (Behavior-Driven Development) <ul><li>Encourages collaboration between developers and non-technicals </li></ul><ul><li>Clear understanding of desired software behaviour </li></ul><ul><li>Combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning </li></ul><ul><li>Focuses on the reason of why the code should be created </li></ul>
    6. 6. RSpec <ul><li>BDD framework for the Ruby Programming Language </li></ul><ul><li>Has own mocking framework </li></ul><ul><li>Similar to a natural language specification </li></ul>
    7. 7. Installation RSpec <ul><li>In Gem-file: </li></ul><ul><li>group :test, :development do </li></ul><ul><li>gem 'rspec-rails' </li></ul><ul><li>end </li></ul><ul><li>Install >gem install rspec-rails </li></ul><ul><li>Install >bundle install </li></ul>
    8. 8. Run RSpec <ul><li>Run >db:migrate </li></ul><ul><li>Run >rake rspec </li></ul>
    9. 9. Run Rspec(cont.) <ul><li>After installation, there is a RSpec directory </li></ul><ul><li>Contains Spec directory of: </li></ul><ul><ul><ul><li>Controllers </li></ul></ul></ul><ul><ul><ul><li>Helpers </li></ul></ul></ul><ul><ul><ul><li>Models </li></ul></ul></ul><ul><ul><ul><li>Requests </li></ul></ul></ul><ul><ul><ul><li>Routings </li></ul></ul></ul><ul><ul><ul><li>Views etc </li></ul></ul></ul>
    10. 10. Example: <ul><li>Suppose, One has some dreams, He wants to list them and wish to complete within 10years. </li></ul><ul><li>So, he has a Controller like “Dream”, </li></ul><ul><li>- Has a Model of the same name. </li></ul><ul><li>- Has a view to see the dreams. </li></ul><ul><li>We are going to test the controllers, models and views mainly if he makes his dream correctly. </li></ul>
    11. 11. RSpec with Controllers <ul><li>Goal is to create very simple, easy to read </li></ul><ul><li>One assertion per test </li></ul><ul><li>Stub everything (actually we use “Factory girl”) </li></ul><ul><li>Emphasis on Controllers and Models to test </li></ul>
    12. 12. RSpec with Controllers(cont.) <ul><li>First, add “spec_helper” in the controller spec file. Like, in “dreams_controller_spec” </li></ul><ul><li>The file contains: </li></ul><ul><ul><ul><li>ENV[&quot;RAILS_ENV&quot;] ||= 'test' </li></ul></ul></ul><ul><ul><ul><li>require File.expand_path(&quot;../../config/environment&quot;, __FILE__) </li></ul></ul></ul><ul><ul><ul><li>require 'rspec/rails‘ </li></ul></ul></ul>
    13. 13. RSpec with Controllers(cont.) <ul><li>First, do the things once, that can be repeated many times. Like: at first, create a Dream instance </li></ul><ul><ul><li>Then add setup </li></ul></ul><ul><ul><li>before(:all) do </li></ul></ul><ul><ul><li>@dream = Dream.create! </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>At the end Teardown </li></ul></ul><ul><ul><li>after(:all) do </li></ul></ul><ul><ul><li>@dream.delete </li></ul></ul><ul><ul><li>end </li></ul></ul>
    14. 14. RSpec with Controllers(cont.) <ul><li>Suppose, he has a show method, that can show a particular dream </li></ul><ul><ul><li>def show </li></ul></ul><ul><ul><li>@dream = Dream.find(params[:id]) </li></ul></ul><ul><ul><li>respond_with(@dream) </li></ul></ul><ul><ul><li>End </li></ul></ul><ul><li>Simply, finds a “Dream” using a particular “id”, show this with “@dream” </li></ul>
    15. 15. RSpec with Controllers(cont.) <ul><li>Now, we’ve to test the show, like, </li></ul><ul><ul><li>describe &quot;GET show&quot; do </li></ul></ul><ul><ul><li>it &quot;assigns the requested dream as @dream&quot; do </li></ul></ul><ul><ul><li>get :show, :id => </li></ul></ul><ul><ul><li>assigns(:dream).should eq(@dream1) </li></ul></ul><ul><ul><li>end </li></ul></ul>
    16. 16. RSpec with Controllers(cont.) <ul><li>First, “show” is a “GET” method, So, we use to describe “GET show” </li></ul><ul><li>Then assigns the “dream” from our test to “@dream” of the controller to test if the match </li></ul><ul><li>Now, because the route like, dreams/:id/show </li></ul><ul><li>- So, we pass :id => </li></ul>
    17. 17. RSpec with Controllers(cont.) <ul><li>Then assigns the “@dream” from test controller to “:dream” in controller (they should be equal!) </li></ul><ul><li>assigns(:dream) @dream=Dream.find(params[:id]) </li></ul><ul><li>should eq(@dream1) @dream </li></ul>
    18. 18. Response with Rspec <ul><li>Some, responses are given below: </li></ul><ul><li>response.should be_success </li></ul><ul><li>response.should render_template(&quot;complete_form&quot;) </li></ul><ul><li>response.should redirect_to(dreams_path) </li></ul>
    19. 19. RSpec with Models <ul><li>Factory Girl: </li></ul><ul><ul><li>Gem, used to create objects </li></ul></ul><ul><ul><li>Similar to use fixtures </li></ul></ul><ul><li>Factory.define :dream, :class=>Dream do |f| </li></ul><ul><li> ‘dreamname1’ </li></ul><ul><li>end </li></ul>
    20. 20. RSpec with Models(cont.) <ul><li>Again, he has a “Dream” Model </li></ul><ul><li>So, we have to create a stub “Dream” model to test this </li></ul><ul><li>This can be done using factory_girl </li></ul><ul><li>Suppose, he has a validation </li></ul><ul><li>- “Every Dream should have a name” </li></ul>
    21. 21. RSpec with Models(cont.) <ul><li>Describe Dream do </li></ul><ul><li>before(:all) do </li></ul><ul><li>@dream=Factory(:dream) </li></ul><ul><li>end </li></ul><ul><li>It “should have name” do </li></ul><ul><li>‘’ </li></ul><ul><li>@dream.should_no be_valid </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
    22. 22. RSpec with Views <ul><li>Suppose, he wants to view all the dreams and has a “index.html” file </li></ul><ul><li>So, again at first, we have to make a stub array of “Dream” model </li></ul><ul><li>And if it renders the “index.html” then, the test is succeed </li></ul>
    23. 23. RSpec with Views(cont.) <ul><li>describe &quot;dreams/index.html.erb&quot; do </li></ul><ul><li>before(:each) do </li></ul><ul><li>assign(:dreams, [ </li></ul><ul><li>stub_model(Dream), </li></ul><ul><li>stub_model(Dream) </li></ul><ul><li>]) </li></ul><ul><li>end </li></ul><ul><li>it &quot;renders a list of dreams&quot; do </li></ul><ul><li>render </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
    24. 24. Rcov <ul><li>Code coverage tool for Ruby </li></ul><ul><li>Viewing overall test unit coverage of target code </li></ul><ul><li>Write gem ‘rcov’ in Gemfile </li></ul><ul><li>Install> gem install rcov </li></ul><ul><li>Run >rake spec:rcov </li></ul><ul><li>From “project/coverage/index.html”, we can see the total coverage in percentage </li></ul>
    25. 25. Rcov Report
    26. 26. <ul><li>Thank You </li></ul>