• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
TDD, BDD, RSpec
 

TDD, BDD, RSpec

on

  • 4,517 views

A quick overview on TDD, BDD and RSpec.

A quick overview on TDD, BDD and RSpec.

Statistics

Views

Total Views
4,517
Views on SlideShare
3,466
Embed Views
1,051

Actions

Likes
4
Downloads
50
Comments
0

3 Embeds 1,051

http://www.nascenia.com 1035
http://localhost 14
http://www.hanrss.com 2

Accessibility

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.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • www.nascenia.com

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

  • Presented by Nazia Hossain Nascenia IT www.nascenia.com
  • Testing in Software Engineering
    • Testing is to determine if the requirements of the application are met
    • Classification:
      • White box testing
      • Black box testing
    www.nascenia.com
  • 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
    www.nascenia.com
  • 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
    www.nascenia.com
  • 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
    www.nascenia.com
  • RSpec
    • BDD framework for the Ruby Programming Language
    • Has own mocking framework
    • Similar to a natural language specification
    www.nascenia.com
  • Installation RSpec
    • In Gem-file:
    • group :test, :development do
    • gem 'rspec-rails'
    • end
    • Install >gem install rspec-rails
    • Install >bundle install
    www.nascenia.com
  • Run RSpec
    • Run >db:migrate
    • Run >rake rspec
    www.nascenia.com
  • Run Rspec(cont.)
    • After installation, there is a RSpec directory
    • Contains Spec directory of:
        • Controllers
        • Helpers
        • Models
        • Requests
        • Routings
        • Views etc
    www.nascenia.com
  • 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.
    www.nascenia.com
  • 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
    www.nascenia.com
  • 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‘
    www.nascenia.com
  • 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
    www.nascenia.com
  • 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”
    www.nascenia.com
  • 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 => @dream1.id.to_s
      • assigns(:dream).should eq(@dream1)
      • end
    www.nascenia.com
  • 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 => @dream.id
    www.nascenia.com
  • 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
    www.nascenia.com
  • Response with Rspec
    • Some, responses are given below:
    • response.should be_success
    • response.should render_template("complete_form")
    • response.should redirect_to(dreams_path)
    www.nascenia.com
  • RSpec with Models
    • Factory Girl:
      • Gem, used to create objects
      • Similar to use fixtures
    • Factory.define :dream, :class=>Dream do |f|
    • f.name ‘dreamname1’
    • end
    www.nascenia.com
  • 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”
    www.nascenia.com
  • RSpec with Models(cont.)
    • Describe Dream do
    • before(:all) do
    • @dream=Factory(:dream)
    • end
    • It “should have name” do
    • @dream.name=‘’
    • @dream.should_no be_valid
    • end
    • end
    www.nascenia.com
  • 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
    www.nascenia.com
  • 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
    www.nascenia.com
  • 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
    www.nascenia.com
  • Rcov Report www.nascenia.com
    • Thank You
    www.nascenia.com