KEMBAR78
Automated testing with RSpec | PDF
automated testing with RSpec


         Fattahul Alam
        www.nascenia.com
RSpec

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




                    ©Nascenia IT
installation
$ gem install rspec
Fetching: …
Successfully installed rspec-core-2.12.2
Successfully installed diff-lcs-1.1.3
Successfully installed rspec-expectations-2.12.1
Successfully installed rspec-mocks-2.12.1
Successfully installed rspec-2.12.0
5 gems installed

$ rspec --init #in project directory

create spec/spec_helper.rb
create .rspec
                                 ©Nascenia IT
describe block
Source code at lib/user.rb
Specifications at spec/lib/user_spec.rb
spec/lib/user_spec.rb


require 'spec_helper'
describe "A User" do

end



                             ©Nascenia IT
describe + it
spec/lib/user_spec.rb



require 'spec_helper'
describe "A User" do
  it "is named Matsumoto"
end




                             ©Nascenia IT
pending
spec/lib/user_spec.rb


require 'spec_helper'
describe "A User" do
  it "is named Matsumoto"
end

$ 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 seconds
1 example, 0 failures, 1 pending
                                   ©Nascenia IT
describe class
spec/lib/user_spec.rb


require 'spec_helper'
describe User do
  it 'is named Matsumoto'
end



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

                                      ©Nascenia IT
create class
spec/lib/user_spec.rb                             lib/user.rb

require 'spec_helper' class User
describe User do            #empty class
  it 'is named Matsumoto' end
end

$ rspec spec/lib/user_spec.rb
*
Pending:
 User is named Matsumoto
  # Not yet implemented
  # ./spec/lib/user_spec.rb:5
Finished in 0.00059 seconds
1 example, 0 failures, 1 pending
                                   ©Nascenia IT
expectations
spec/lib/user_spec.rb

require "spec_helper"
require 'user'
describe User do
  it 'is named Matsumoto' do
      user = User.new
      user.name.should == "Matsumoto"
  end
end
$ rspec spec/lib/user_spec.rb
F
Failures:
 1) User is named Matsumoto
   Failure/Error: user.name.should == "Matsumoto“
   NoMethodError: undefined method `name' for #<User:0x0000000129e540>…
                                  ©Nascenia IT
success!
spec/lib/user_spec.rb
                                                  lib/user.rb
require "spec_helper"                              class User
require 'user'                                       attr_accessor :name
describe User do
   it 'is named Matsumoto' do                          def initialize
       user = User.new
                                                                @name = 'Matsumoto'
         user.name.should == "Matsumoto"
   end                                               end
end                                                end


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


                                   ©Nascenia IT
expectation (one more!)
spec/lib/user_spec.rb                          lib/user.rb

require "spec_helper"                          class User
require 'user'                                    attr_accessor :name,
describe User do                                 :heads
    ...
  it 'has one head' do                           def initialize
       user = User.new                               @name =
       user.heads.should > 0                     'Matsumoto'
  end                                                @heads = 1
end                                              end
                                               end


$ rspec spec/lib/user_spec.rb
..
Finished in 0.00049 seconds
2 examples, 0 failures
                                ©Nascenia IT
more matchers
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
pending
it 'is named Matsumoto' #It without body

xit 'is named Matsumoto' do
  failed codes
end

it 'is named Matsumoto' do
  pending
  failed codes
end
                 ©Nascenia IT
with rails
Gemfile
group :development, :test do
  gem 'rspec-rails'
end

$ bundle install

$ rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
                           ©Nascenia IT
configuration
spec/spec_helper.rb

...
# 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, flexmock
  or RR, uncomment the appropriate line:
  # config.mock_with :mocha
  config.mock_with :rspec
end
                           ©Nascenia IT
execution
$ 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
model spec
spec/models/user_spec.rb
require 'spec_helper'
describe User do
  it "is not valid without name" do
    user = User.new
    user.should_not be_valid
  end
end
$ rspec spec/models/user_spec.rb
F
Failures:
 1) User is not valid without name
…
                                     ©Nascenia IT
model spec
spec/models/user_spec.rb               app/models/user.rb
require 'spec_helper'                  class User <
describe User do
   it "is not valid without            ActiveRecord::Base
    name" do                             …
     user = User.new                   validates :name, presence: true
     user.should_not be_valid
  end                                  end
end




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


                                   ©Nascenia IT
hook
spec/models/user_spec.rb                       spec/models/user_spec.rb
describe User do                             describe User do
  let(:user){ User.new }                       let(:user){ User.new }
                                               before { user.active! }
  it "is active" do
    user.active!                                   it "is active" do
     user.should be_active                           user.should be_active
  end                                              end

  it "can login" do                                it "can login" do
    user.active!                                     user.should be_able_to_login
     user.should be_able_to_login              end
  end                                        end
end

                                    ©Nascenia IT
hook (Contd.)
before(:each)

before(:all)

after(:each)

after(:all)

                     ©Nascenia IT
hook in context
spec/models/user_spec.rb
describe User do
  let(:user){ User.new }
  before { user.active! }
...
context "with a published profile" do
    it "allows profile to be visible by whitelist subscribers" do
      user.profile.publish!
      ...
    end

 it "does not allow profile to be visible by blacklist subscribers" do
      user.profile.publish!
      ...
    end
  end
end




                               ©Nascenia IT
hook in context
spec/models/user_spec.rb
describe User do
  let(: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
       ...
     end
     it "does not allow profile to be visible by blacklist subscribers"
    do
       ...
     end
  end
end




                                ©Nascenia IT
Custom matchers
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
keep in touch!



facebook.com/nascenia
     @NasceniaIT



        ©Nascenia IT

Automated testing with RSpec

  • 1.
    automated testing withRSpec Fattahul Alam www.nascenia.com
  • 2.
    RSpec • A popularruby testing framework • Less Ruby-like, natural syntax • Well formatted output ©Nascenia IT
  • 3.
    installation $ gem installrspec Fetching: … Successfully installed rspec-core-2.12.2 Successfully installed diff-lcs-1.1.3 Successfully installed rspec-expectations-2.12.1 Successfully installed rspec-mocks-2.12.1 Successfully installed rspec-2.12.0 5 gems installed $ rspec --init #in project directory create spec/spec_helper.rb create .rspec ©Nascenia IT
  • 4.
    describe block Source codeat lib/user.rb Specifications at spec/lib/user_spec.rb spec/lib/user_spec.rb require 'spec_helper' describe "A User" do end ©Nascenia IT
  • 5.
    describe + it spec/lib/user_spec.rb require'spec_helper' describe "A User" do it "is named Matsumoto" end ©Nascenia IT
  • 6.
    pending spec/lib/user_spec.rb require 'spec_helper' describe "AUser" do it "is named Matsumoto" end $ 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 seconds 1 example, 0 failures, 1 pending ©Nascenia IT
  • 7.
    describe class spec/lib/user_spec.rb require 'spec_helper' describeUser do it 'is named Matsumoto' end $ rspec spec/lib/user_spec.rb user_spec.rb:3:in `<top (required)>': uninitialized constant User (NameError) ©Nascenia IT
  • 8.
    create class spec/lib/user_spec.rb lib/user.rb require 'spec_helper' class User describe User do #empty class it 'is named Matsumoto' end end $ rspec spec/lib/user_spec.rb * Pending: User is named Matsumoto # Not yet implemented # ./spec/lib/user_spec.rb:5 Finished in 0.00059 seconds 1 example, 0 failures, 1 pending ©Nascenia IT
  • 9.
    expectations spec/lib/user_spec.rb require "spec_helper" require 'user' describeUser do it 'is named Matsumoto' do user = User.new user.name.should == "Matsumoto" end end $ rspec spec/lib/user_spec.rb F Failures: 1) User is named Matsumoto Failure/Error: user.name.should == "Matsumoto“ NoMethodError: undefined method `name' for #<User:0x0000000129e540>… ©Nascenia IT
  • 10.
    success! spec/lib/user_spec.rb lib/user.rb require "spec_helper" class User require 'user' attr_accessor :name describe User do it 'is named Matsumoto' do def initialize user = User.new @name = 'Matsumoto' user.name.should == "Matsumoto" end end end end $ rspec spec/lib/user_spec.rb . Finished in 0.00074 seconds 1 example, 0 failures ©Nascenia IT
  • 11.
    expectation (one more!) spec/lib/user_spec.rb lib/user.rb require "spec_helper" class User require 'user' attr_accessor :name, describe User do :heads ... it 'has one head' do def initialize user = User.new @name = user.heads.should > 0 'Matsumoto' end @heads = 1 end end end $ rspec spec/lib/user_spec.rb .. Finished in 0.00049 seconds 2 examples, 0 failures ©Nascenia IT
  • 12.
    more matchers 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
  • 13.
    pending it 'is namedMatsumoto' #It without body xit 'is named Matsumoto' do failed codes end it 'is named Matsumoto' do pending failed codes end ©Nascenia IT
  • 14.
    with rails Gemfile group :development,:test do gem 'rspec-rails' end $ bundle install $ rails generate rspec:install create .rspec create spec create spec/spec_helper.rb ©Nascenia IT
  • 15.
    configuration spec/spec_helper.rb ... # 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, flexmock or RR, uncomment the appropriate line: # config.mock_with :mocha config.mock_with :rspec end ©Nascenia IT
  • 16.
    execution $ 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
  • 17.
    model spec spec/models/user_spec.rb require 'spec_helper' describeUser do it "is not valid without name" do user = User.new user.should_not be_valid end end $ rspec spec/models/user_spec.rb F Failures: 1) User is not valid without name … ©Nascenia IT
  • 18.
    model spec spec/models/user_spec.rb app/models/user.rb require 'spec_helper' class User < describe User do it "is not valid without ActiveRecord::Base name" do … user = User.new validates :name, presence: true user.should_not be_valid end end end $ rspec spec/models/user_spec.rb . Finished in 0.08148 seconds 1 example, 0 failures ©Nascenia IT
  • 19.
    hook spec/models/user_spec.rb spec/models/user_spec.rb describe User do describe User do let(:user){ User.new } let(:user){ User.new } before { user.active! } it "is active" do user.active! it "is active" do user.should be_active user.should be_active end end it "can login" do it "can login" do user.active! user.should be_able_to_login user.should be_able_to_login end end end end ©Nascenia IT
  • 20.
  • 21.
    hook in context spec/models/user_spec.rb describeUser do let(:user){ User.new } before { user.active! } ... context "with a published profile" do it "allows profile to be visible by whitelist subscribers" do user.profile.publish! ... end it "does not allow profile to be visible by blacklist subscribers" do user.profile.publish! ... end end end ©Nascenia IT
  • 22.
    hook in context spec/models/user_spec.rb describeUser do let(: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 ... end it "does not allow profile to be visible by blacklist subscribers" do ... end end end ©Nascenia IT
  • 23.
    Custom matchers 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
  • 24.
    keep in touch! facebook.com/nascenia @NasceniaIT ©Nascenia IT