26
Presented by Nazia Hossain Nascenia IT www.nascenia.com

TDD, BDD, RSpec

Embed Size (px)

DESCRIPTION

A quick overview on TDD, BDD and RSpec.

Citation preview

Page 1: TDD, BDD, RSpec

Presented byNazia Hossain

Nascenia IT

www.nascenia.com

Page 2: TDD, BDD, RSpec

Testing in Software EngineeringTesting is to determine if the requirements of

the application are met

Classification:White box testingBlack box testing

www.nascenia.com

Page 3: TDD, BDD, RSpec

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

Page 4: TDD, BDD, RSpec

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

Page 5: TDD, BDD, RSpec

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

Page 6: TDD, BDD, RSpec

RSpecBDD framework for the Ruby Programming

Language

Has own mocking framework

Similar to a natural language specification

www.nascenia.com

Page 7: TDD, BDD, RSpec

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

Install>gem install rspec-railsInstall>bundle install

www.nascenia.com

Page 8: TDD, BDD, RSpec

Run RSpecRun>db:migrate

Run>rake rspec

www.nascenia.com

Page 9: TDD, BDD, RSpec

Run Rspec(cont.)After installation, there is a RSpec directoryContains Spec directory of:

Controllers Helpers Models Requests Routings Views etc

www.nascenia.com

Page 10: TDD, BDD, RSpec

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

Page 11: TDD, BDD, RSpec

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

Page 12: TDD, BDD, RSpec

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

Page 13: TDD, BDD, RSpec

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

Page 14: TDD, BDD, RSpec

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

Page 15: TDD, BDD, RSpec

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

Page 16: TDD, BDD, RSpec

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

Page 17: TDD, BDD, RSpec

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

Page 18: TDD, BDD, RSpec

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

Page 19: TDD, BDD, RSpec

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

Page 20: TDD, BDD, RSpec

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

Page 21: TDD, BDD, RSpec

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

Page 22: TDD, BDD, RSpec

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

Page 23: TDD, BDD, RSpec

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

Page 24: TDD, BDD, RSpec

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

Page 25: TDD, BDD, RSpec

Rcov Report

www.nascenia.com

Page 26: TDD, BDD, RSpec

Thank You

www.nascenia.com