TDD, BDD, RSpec

4,775 views
4,572 views

Published on

A quick overview on TDD, BDD and RSpec.

Published in: Technology, Spiritual
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,775
On SlideShare
0
From Embeds
0
Number of Embeds
1,077
Actions
Shares
0
Downloads
68
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • www.nascenia.com
  • TDD, BDD, RSpec

    1. 1. Presented by Nazia Hossain Nascenia IT www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    8. 8. Run RSpec <ul><li>Run >db:migrate </li></ul><ul><li>Run >rake rspec </li></ul>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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 => @dream1.id.to_s </li></ul></ul><ul><ul><li>assigns(:dream).should eq(@dream1) </li></ul></ul><ul><ul><li>end </li></ul></ul>www.nascenia.com
    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 => @dream.id </li></ul>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>f.name ‘dreamname1’ </li></ul><ul><li>end </li></ul>www.nascenia.com
    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>www.nascenia.com
    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>@dream.name=‘’ </li></ul><ul><li>@dream.should_no be_valid </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    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>www.nascenia.com
    25. 25. Rcov Report www.nascenia.com
    26. 26. <ul><li>Thank You </li></ul>www.nascenia.com

    ×