automated testing with rspec

24
automated testing with RSpec Fattahul Alam www.nascenia.com

Upload: nascenia-it

Post on 06-May-2015

2.483 views

Category:

Documents


4 download

DESCRIPTION

Fattahul Alam, CTO of Nascenia IT talked about automated testing with RSpec during Ruby Conf Bangladesh 2013 ( http://www.rubyconfbd.org )

TRANSCRIPT

Page 1: Automated testing with RSpec

automated testing with RSpec

Fattahul Alamwww.nascenia.com

Page 2: Automated testing with RSpec

• A popular ruby testing framework• Less Ruby-like, natural syntax• Well formatted output

©Nascenia IT

RSpec

Page 3: Automated testing with RSpec

$ gem install rspecFetching: …Successfully installed rspec-core-2.12.2Successfully installed diff-lcs-1.1.3Successfully installed rspec-expectations-2.12.1Successfully installed rspec-mocks-2.12.1Successfully installed rspec-2.12.05 gems installed

©Nascenia IT

$ rspec --init #in project directory

create spec/spec_helper.rbcreate .rspec

installation

Page 4: Automated testing with RSpec

Source code at lib/user.rbSpecifications at spec/lib/user_spec.rb

©Nascenia IT

require 'spec_helper'describe "A User" do

end

spec/lib/user_spec.rb

describe block

Page 5: Automated testing with RSpec

require 'spec_helper'

describe "A User" do

it "is named Matsumoto"

end

©Nascenia IT

spec/lib/user_spec.rb

describe + it

Page 6: Automated testing with RSpec

require 'spec_helper'

describe "A User" do

it "is named Matsumoto"

end

©Nascenia IT

$ rspec spec/lib/user_spec.rb*Pending:

A User is named Matsumoto# Not yet implemented# ./spec/lib/user_spec.rb:4

Finished in 0.00026 seconds1 example, 0 failures, 1 pending

spec/lib/user_spec.rb

pending

Page 7: Automated testing with RSpec

require 'spec_helper'

describe User do

it 'is named Matsumoto'

end

©Nascenia IT

$ rspec spec/lib/user_spec.rbuser_spec.rb:3:in `<top (required)>': uninitialized constant User (NameError)

spec/lib/user_spec.rb

describe class

Page 8: Automated testing with RSpec

require 'spec_helper'

describe User do

it 'is named Matsumoto'

end

©Nascenia IT

$ rspec spec/lib/user_spec.rb*Pending:

User is named Matsumoto# Not yet implemented# ./spec/lib/user_spec.rb:5

Finished in 0.00059 seconds1 example, 0 failures, 1 pending

class User

#empty class

end

spec/lib/user_spec.rb lib/user.rb

create class

Page 9: Automated testing with RSpec

require "spec_helper"require 'user'describe User do

it 'is named Matsumoto' douser = User.newuser.name.should == "Matsumoto"

endend

©Nascenia IT

$ rspec spec/lib/user_spec.rbFFailures:

1) User is named MatsumotoFailure/Error: user.name.should == "Matsumoto“NoMethodError: undefined method `name' for #<User:0x0000000129e540>…

spec/lib/user_spec.rb

expectations

Page 10: Automated testing with RSpec

require "spec_helper"require 'user'describe User do

it 'is named Matsumoto' douser = User.newuser.name.should == "Matsumoto"

endend

©Nascenia IT

class Userattr_accessor :name

def initialize@name = 'Matsumoto'

endend

$ rspec spec/lib/user_spec.rb.Finished in 0.00074 seconds1 example, 0 failures

spec/lib/user_spec.rblib/user.rb

success!

Page 11: Automated testing with RSpec

require "spec_helper"require 'user'describe User do

...it 'has one head' do

user = User.newuser.heads.should > 0

end end

©Nascenia IT

class Userattr_accessor :name, :heads

def initialize@name =

'Matsumoto'@heads = 1

endend

$ rspec spec/lib/user_spec.rb..Finished in 0.00049 seconds2 examples, 0 failures

spec/lib/user_spec.rb lib/user.rb

expectation (one more!)

Page 12: Automated testing with RSpec

user.name.should == "Matsumoto"

user.fit_for_drive.should == true

user.fit_for_drive.should be_true

user.height.should be < 7.5

user.length.should_not >= 7.5

©Nascenia IT

more matchers

Page 13: Automated testing with RSpec

it 'is named Matsumoto' #It without body

xit 'is named Matsumoto' dofailed codes

end

it 'is named Matsumoto' dopendingfailed codes

end

©Nascenia IT

pending

Page 14: Automated testing with RSpec

Gemfilegroup :development, :test do

gem 'rspec-rails'end

$ bundle install

$ rails generate rspec:installcreate .rspeccreate speccreate spec/spec_helper.rb

©Nascenia IT

with rails

Page 15: Automated testing with RSpec

...# in spec/support/ and its subdirectories.Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|# If you prefer to use mocha, flexmockor RR, uncomment the appropriate line:# config.mock_with :mochaconfig.mock_with :rspec

end

©Nascenia IT

spec/spec_helper.rb

configuration

Page 16: Automated testing with RSpec

$ rspec# runs all _spec.rb file under spec directory

$ rspec spec/models# runs all specs under a specific directory

$ rspec spec/models/user_spec.rb# runs a specific spec file

$ rspec spec/models/user_spec.rb:4# runs the closest example around that line.

©Nascenia IT

execution

Page 17: Automated testing with RSpec

require 'spec_helper'describe User do

it "is not valid without name" douser = User.newuser.should_not be_valid

endend

©Nascenia IT

spec/models/user_spec.rb

$ rspec spec/models/user_spec.rbFFailures:

1) User is not valid without name…

model spec

Page 18: Automated testing with RSpec

require 'spec_helper'describe User doit "is not valid without name" douser = User.newuser.should_not be_valid

endend

©Nascenia IT

class User < ActiveRecord::Base…

validates :name, presence: true

end

$ rspec spec/models/user_spec.rb.Finished in 0.08148 seconds1 example, 0 failures

spec/models/user_spec.rb app/models/user.rb

model spec

Page 19: Automated testing with RSpec

describe User dolet(:user){ User.new }

it "is active" douser.active!user.should be_active

end

it "can login" douser.active!user.should be_able_to_login

endend

©Nascenia IT

describe User dolet(:user){ User.new }before { user.active! }

it "is active" douser.should be_active

end

it "can login" douser.should be_able_to_login

endend

spec/models/user_spec.rb spec/models/user_spec.rb

hook

Page 20: Automated testing with RSpec

before(:each)

before(:all)

after(:each)

after(:all)

©Nascenia IT

hook (Contd.)

Page 21: Automated testing with RSpec

describe User dolet(:user){ User.new }before { user.active! }

...context "with a published profile" do

it "allows profile to be visible by whitelist subscribers" douser.profile.publish!...

end

it "does not allow profile to be visible by blacklist subscribers" douser.profile.publish!...

endend

end

©Nascenia IT

spec/models/user_spec.rb

hook in context

Page 22: Automated testing with RSpec

describe User dolet(:user){ User.new }before { user.active! }

...context "with a published profile" do

before { user.profile.publish! }

it "allows profile to be visible by whitelist subscribers" do...

endit "does not allow profile to be visible by blacklist subscribers" do

...end

endend

©Nascenia IT

spec/models/user_spec.rb

hook in context

Page 23: Automated testing with RSpec

it { should validate_presence_of(:name) }

it { should ensure_inclusion_of(:age).in_range(18..25) }

it { should have_many(:tweets).dependent(:destroy) }

©Nascenia IT

Custom matchers

Page 24: Automated testing with RSpec

facebook.com/nascenia@NasceniaIT

©Nascenia IT

keep in touch!