Upcoming SlideShare
Loading in...5




A quick overview on TDD, BDD and RSpec.

A quick overview on TDD, BDD and RSpec.



Total Views
Views on SlideShare
Embed Views



3 Embeds 1,054 1038
http://localhost 14 2


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

TDD, BDD, RSpec TDD, BDD, RSpec Presentation Transcript

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