TDD, BDD, RSpec

Preview:

DESCRIPTION

A quick overview on TDD, BDD and RSpec.

Citation preview

Presented byNazia Hossain

Nascenia IT

www.nascenia.com

Testing in Software EngineeringTesting is to determine if the requirements of

the application are met

Classification:White box testingBlack box testing

www.nascenia.com

ClassificationWhite 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

1. Write some code2. Run the automated tests and see them

succeed3. Refactor code4. 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

RSpecBDD framework for the Ruby Programming

Language

Has own mocking framework

Similar to a natural language specification

www.nascenia.com

Installation RSpecIn Gem-file: group :test, :development do gem 'rspec-rails' end

Install>gem install rspec-railsInstall>bundle install

www.nascenia.com

Run RSpecRun>db:migrate

Run>rake rspec

www.nascenia.com

Run Rspec(cont.)After installation, there is a RSpec directoryContains 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 ControllersGoal is to create very simple, easy to readOne assertion per testStub 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! endAt 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 dreamdef 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 RspecSome, responses are given below:

response.should be_successresponse.should

render_template("complete_form")response.should redirect_to(dreams_path)

www.nascenia.com

RSpec with ModelsFactory 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 endend

www.nascenia.com

RSpec with ViewsSuppose, he wants to view all the dreams and

has a “index.html” fileSo, 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 endend

www.nascenia.com

RcovCode coverage tool for RubyViewing overall test unit coverage of target

codeWrite gem ‘rcov’ in GemfileInstall>gem install rcovRun>rake spec:rcovFrom “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