SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

RSpec and Rails
RSpec and RailsRSpec and Rails
RSpec and RailsAlan Hecht
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + djangoNina Zakharenko
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST FrameworkLoad Impact
 
RSpec User Stories
RSpec User StoriesRSpec User Stories
RSpec User Storiesrahoulb
 
Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101Roy Yu
 
Testing JS with Jasmine
Testing JS with JasmineTesting JS with Jasmine
Testing JS with JasmineEvgeny Gurin
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with JasmineTim Tyrrell
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesNina Zakharenko
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScriptpamselle
 
MidwestJS Zero to Testing
MidwestJS Zero to TestingMidwestJS Zero to Testing
MidwestJS Zero to Testingpamselle
 
Intro to Angular.JS Directives
Intro to Angular.JS DirectivesIntro to Angular.JS Directives
Intro to Angular.JS DirectivesChristian Lilley
 
Python for AngularJS
Python for AngularJSPython for AngularJS
Python for AngularJSJeff Schenck
 
Intro to-rails-webperf
Intro to-rails-webperfIntro to-rails-webperf
Intro to-rails-webperfNew Relic
 

Was ist angesagt? (20)

RSpec and Rails
RSpec and RailsRSpec and Rails
RSpec and Rails
 
TDD with phpspec2
TDD with phpspec2TDD with phpspec2
TDD with phpspec2
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST Framework
 
RSpec User Stories
RSpec User StoriesRSpec User Stories
RSpec User Stories
 
Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101
 
Testing JS with Jasmine
Testing JS with JasmineTesting JS with Jasmine
Testing JS with Jasmine
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with Jasmine
 
TDD, BDD, RSpec
TDD, BDD, RSpecTDD, BDD, RSpec
TDD, BDD, RSpec
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScript
 
MidwestJS Zero to Testing
MidwestJS Zero to TestingMidwestJS Zero to Testing
MidwestJS Zero to Testing
 
RSpec
RSpecRSpec
RSpec
 
Excellent
ExcellentExcellent
Excellent
 
TDD with PhpSpec
TDD with PhpSpecTDD with PhpSpec
TDD with PhpSpec
 
RSpec. Part 2
RSpec. Part 2RSpec. Part 2
RSpec. Part 2
 
Rails is not just Ruby
Rails is not just RubyRails is not just Ruby
Rails is not just Ruby
 
Intro to Angular.JS Directives
Intro to Angular.JS DirectivesIntro to Angular.JS Directives
Intro to Angular.JS Directives
 
Python for AngularJS
Python for AngularJSPython for AngularJS
Python for AngularJS
 
Intro to-rails-webperf
Intro to-rails-webperfIntro to-rails-webperf
Intro to-rails-webperf
 

Andere mochten auch

Testing with Rspec 3
Testing with Rspec 3Testing with Rspec 3
Testing with Rspec 3David Paluy
 
Rails testing: factories or fixtures?
Rails testing: factories or fixtures?Rails testing: factories or fixtures?
Rails testing: factories or fixtures?mtoppa
 
How to improve our acceptance tests - Pyccuracy VS Splinter
How to improve our acceptance tests - Pyccuracy VS SplinterHow to improve our acceptance tests - Pyccuracy VS Splinter
How to improve our acceptance tests - Pyccuracy VS SplinterFernando Sandes
 
29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview QuestionsArc & Codementor
 

Andere mochten auch (9)

Basic RSpec 2
Basic RSpec 2Basic RSpec 2
Basic RSpec 2
 
Testing with Rspec 3
Testing with Rspec 3Testing with Rspec 3
Testing with Rspec 3
 
[RuPy 2011] Automatic Acceptance Tests in Ruby
[RuPy 2011] Automatic Acceptance Tests in Ruby[RuPy 2011] Automatic Acceptance Tests in Ruby
[RuPy 2011] Automatic Acceptance Tests in Ruby
 
Acceptance tests
Acceptance testsAcceptance tests
Acceptance tests
 
MacRuby
MacRubyMacRuby
MacRuby
 
Rails testing: factories or fixtures?
Rails testing: factories or fixtures?Rails testing: factories or fixtures?
Rails testing: factories or fixtures?
 
How to improve our acceptance tests - Pyccuracy VS Splinter
How to improve our acceptance tests - Pyccuracy VS SplinterHow to improve our acceptance tests - Pyccuracy VS Splinter
How to improve our acceptance tests - Pyccuracy VS Splinter
 
Rspec 101
Rspec 101Rspec 101
Rspec 101
 
29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions
 

Ähnlich wie RSpec testing framework for Ruby

Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
 
Building maintainable javascript applications
Building maintainable javascript applicationsBuilding maintainable javascript applications
Building maintainable javascript applicationsequisodie
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends旻琦 潘
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Prxibbar
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
QConSP 2015 - Dicas de Performance para Aplicações Web
QConSP 2015 - Dicas de Performance para Aplicações WebQConSP 2015 - Dicas de Performance para Aplicações Web
QConSP 2015 - Dicas de Performance para Aplicações WebFabio Akita
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud CastlesBen Scofield
 
浜松Rails3道場 其の参 Controller編
浜松Rails3道場 其の参 Controller編浜松Rails3道場 其の参 Controller編
浜松Rails3道場 其の参 Controller編Masakuni Kato
 
Release responsibly (Maintaining Backwards Compatibility)
Release responsibly (Maintaining Backwards Compatibility)Release responsibly (Maintaining Backwards Compatibility)
Release responsibly (Maintaining Backwards Compatibility)Emily Stolfo
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversBrian Gesiak
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
Build Web Apps using Node.js
Build Web Apps using Node.jsBuild Web Apps using Node.js
Build Web Apps using Node.jsdavidchubbs
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)James Titcumb
 
Associations & JavaScript
Associations & JavaScriptAssociations & JavaScript
Associations & JavaScriptJoost Elfering
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)James Titcumb
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)James Titcumb
 
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina ZakharenkoElegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina ZakharenkoNina Zakharenko
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 

Ähnlich wie RSpec testing framework for Ruby (20)

Controller specs
Controller specsController specs
Controller specs
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Building maintainable javascript applications
Building maintainable javascript applicationsBuilding maintainable javascript applications
Building maintainable javascript applications
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Rails2 Pr
Rails2 PrRails2 Pr
Rails2 Pr
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
QConSP 2015 - Dicas de Performance para Aplicações Web
QConSP 2015 - Dicas de Performance para Aplicações WebQConSP 2015 - Dicas de Performance para Aplicações Web
QConSP 2015 - Dicas de Performance para Aplicações Web
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud Castles
 
浜松Rails3道場 其の参 Controller編
浜松Rails3道場 其の参 Controller編浜松Rails3道場 其の参 Controller編
浜松Rails3道場 其の参 Controller編
 
Release responsibly (Maintaining Backwards Compatibility)
Release responsibly (Maintaining Backwards Compatibility)Release responsibly (Maintaining Backwards Compatibility)
Release responsibly (Maintaining Backwards Compatibility)
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Build Web Apps using Node.js
Build Web Apps using Node.jsBuild Web Apps using Node.js
Build Web Apps using Node.js
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
 
Associations & JavaScript
Associations & JavaScriptAssociations & JavaScript
Associations & JavaScript
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina ZakharenkoElegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 

Mehr von Nascenia IT

Introduction to basic data analytics tools
Introduction to basic data analytics toolsIntroduction to basic data analytics tools
Introduction to basic data analytics toolsNascenia IT
 
Communication workshop in nascenia
Communication workshop in nasceniaCommunication workshop in nascenia
Communication workshop in nasceniaNascenia IT
 
The Art of Statistical Deception
The Art of Statistical DeceptionThe Art of Statistical Deception
The Art of Statistical DeceptionNascenia IT
 
করোনায় কী করি!
করোনায় কী করি!করোনায় কী করি!
করোনায় কী করি!Nascenia IT
 
GDPR compliance expectations from the development team
GDPR compliance expectations from the development teamGDPR compliance expectations from the development team
GDPR compliance expectations from the development teamNascenia IT
 
Writing Clean Code
Writing Clean CodeWriting Clean Code
Writing Clean CodeNascenia IT
 
History & Introduction of Neural Network and use of it in Computer Vision
History & Introduction of Neural Network and use of it in Computer VisionHistory & Introduction of Neural Network and use of it in Computer Vision
History & Introduction of Neural Network and use of it in Computer VisionNascenia IT
 
Ruby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineRuby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineNascenia IT
 
iphone 11 new features
iphone 11 new featuresiphone 11 new features
iphone 11 new featuresNascenia IT
 
Software quality assurance and cyber security
Software quality assurance and cyber securitySoftware quality assurance and cyber security
Software quality assurance and cyber securityNascenia IT
 
Job Market Scenario For Freshers
Job Market Scenario For Freshers Job Market Scenario For Freshers
Job Market Scenario For Freshers Nascenia IT
 
Modern Frontend Technologies (BEM, Retina)
Modern Frontend Technologies (BEM, Retina)Modern Frontend Technologies (BEM, Retina)
Modern Frontend Technologies (BEM, Retina)Nascenia IT
 
CSS for Developers
CSS for DevelopersCSS for Developers
CSS for DevelopersNascenia IT
 
Big commerce app development
Big commerce app developmentBig commerce app development
Big commerce app developmentNascenia IT
 
Integrating QuickBooks Desktop with Rails Application
Integrating QuickBooks Desktop with Rails ApplicationIntegrating QuickBooks Desktop with Rails Application
Integrating QuickBooks Desktop with Rails ApplicationNascenia IT
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideNascenia IT
 
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsRuby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsNascenia IT
 
COREXIT: Microsoft’s new cross platform framework
COREXIT: Microsoft’s new cross platform frameworkCOREXIT: Microsoft’s new cross platform framework
COREXIT: Microsoft’s new cross platform frameworkNascenia IT
 

Mehr von Nascenia IT (20)

Introduction to basic data analytics tools
Introduction to basic data analytics toolsIntroduction to basic data analytics tools
Introduction to basic data analytics tools
 
Communication workshop in nascenia
Communication workshop in nasceniaCommunication workshop in nascenia
Communication workshop in nascenia
 
The Art of Statistical Deception
The Art of Statistical DeceptionThe Art of Statistical Deception
The Art of Statistical Deception
 
করোনায় কী করি!
করোনায় কী করি!করোনায় কী করি!
করোনায় কী করি!
 
GDPR compliance expectations from the development team
GDPR compliance expectations from the development teamGDPR compliance expectations from the development team
GDPR compliance expectations from the development team
 
Writing Clean Code
Writing Clean CodeWriting Clean Code
Writing Clean Code
 
History & Introduction of Neural Network and use of it in Computer Vision
History & Introduction of Neural Network and use of it in Computer VisionHistory & Introduction of Neural Network and use of it in Computer Vision
History & Introduction of Neural Network and use of it in Computer Vision
 
Ruby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineRuby on Rails: Coding Guideline
Ruby on Rails: Coding Guideline
 
iphone 11 new features
iphone 11 new featuresiphone 11 new features
iphone 11 new features
 
Software quality assurance and cyber security
Software quality assurance and cyber securitySoftware quality assurance and cyber security
Software quality assurance and cyber security
 
Job Market Scenario For Freshers
Job Market Scenario For Freshers Job Market Scenario For Freshers
Job Market Scenario For Freshers
 
Modern Frontend Technologies (BEM, Retina)
Modern Frontend Technologies (BEM, Retina)Modern Frontend Technologies (BEM, Retina)
Modern Frontend Technologies (BEM, Retina)
 
CSS for Developers
CSS for DevelopersCSS for Developers
CSS for Developers
 
Big commerce app development
Big commerce app developmentBig commerce app development
Big commerce app development
 
Integrating QuickBooks Desktop with Rails Application
Integrating QuickBooks Desktop with Rails ApplicationIntegrating QuickBooks Desktop with Rails Application
Integrating QuickBooks Desktop with Rails Application
 
Shopify
ShopifyShopify
Shopify
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
 
Clean code
Clean codeClean code
Clean code
 
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsRuby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
 
COREXIT: Microsoft’s new cross platform framework
COREXIT: Microsoft’s new cross platform frameworkCOREXIT: Microsoft’s new cross platform framework
COREXIT: Microsoft’s new cross platform framework
 

RSpec testing framework for Ruby

  • 1. automated testing with RSpec Fattahul Alam www.nascenia.com
  • 2. RSpec • A popular ruby testing framework • Less Ruby-like, natural syntax • Well formatted output ©Nascenia IT
  • 3. 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
  • 4. 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
  • 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 "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
  • 7. 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
  • 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' 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
  • 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 named Matsumoto' #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' 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
  • 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
  • 21. 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
  • 22. 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
  • 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