Your SlideShare is downloading. ×
0
automated testing with RSpec         Fattahul Alam        www.nascenia.com
RSpec• A popular ruby testing framework• Less Ruby-like, natural syntax• Well formatted output                    ©Nasceni...
installation$ gem install rspecFetching: …Successfully installed rspec-core-2.12.2Successfully installed diff-lcs-1.1.3Suc...
describe blockSource code at lib/user.rbSpecifications at spec/lib/user_spec.rbspec/lib/user_spec.rbrequire spec_helperdes...
describe + itspec/lib/user_spec.rbrequire spec_helperdescribe "A User" do  it "is named Matsumoto"end                     ...
pendingspec/lib/user_spec.rbrequire spec_helperdescribe "A User" do  it "is named Matsumoto"end$ rspec spec/lib/user_spec....
describe classspec/lib/user_spec.rbrequire spec_helperdescribe User do  it is named Matsumotoend$ rspec spec/lib/user_spec...
create classspec/lib/user_spec.rb                             lib/user.rbrequire spec_helper class Userdescribe User do   ...
expectationsspec/lib/user_spec.rbrequire "spec_helper"require userdescribe User do  it is named Matsumoto do      user = U...
success!spec/lib/user_spec.rb                                                  lib/user.rbrequire "spec_helper"           ...
expectation (one more!)spec/lib/user_spec.rb                          lib/user.rbrequire "spec_helper"                    ...
more matchersuser.name.should == "Matsumoto"user.fit_for_drive.should == trueuser.fit_for_drive.should be_trueuser.height....
pendingit is named Matsumoto #It without bodyxit is named Matsumoto do  failed codesendit is named Matsumoto do  pending  ...
with railsGemfilegroup :development, :test do  gem rspec-railsend$ bundle install$ rails generate rspec:installcreate .rsp...
configurationspec/spec_helper.rb...# in spec/support/ and its subdirectories.Dir[Rails.root.join("spec/support/**/*.rb"   ...
execution$ rspec   # runs all _spec.rb file under spec directory$ rspec spec/models   # runs all specs under a specific di...
model specspec/models/user_spec.rbrequire spec_helperdescribe User do  it "is not valid without name" do    user = User.ne...
model specspec/models/user_spec.rb               app/models/user.rbrequire spec_helper                  class User <descri...
hookspec/models/user_spec.rb                       spec/models/user_spec.rbdescribe User do                             de...
hook (Contd.)before(:each)before(:all)after(:each)after(:all)                     ©Nascenia IT
hook in contextspec/models/user_spec.rbdescribe User do  let(:user){ User.new }  before { user.active! }...context "with a...
hook in contextspec/models/user_spec.rbdescribe User do  let(:user){ User.new }  before { user.active! }...context "with a...
Custom matchersit { should validate_presence_of(:name) }it { should ensure_inclusion_of(:age).in_range(18..25) }it { shoul...
keep in touch!facebook.com/nascenia     @NasceniaIT        ©Nascenia IT
Upcoming SlideShare
Loading in...5
×

Automated testing with RSpec

2,690

Published on

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

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,690
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Automated testing with RSpec"

  1. 1. automated testing with RSpec Fattahul Alam www.nascenia.com
  2. 2. RSpec• A popular ruby testing framework• Less Ruby-like, natural syntax• Well formatted output ©Nascenia IT
  3. 3. installation$ 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$ rspec --init #in project directorycreate spec/spec_helper.rbcreate .rspec ©Nascenia IT
  4. 4. describe blockSource code at lib/user.rbSpecifications at spec/lib/user_spec.rbspec/lib/user_spec.rbrequire spec_helperdescribe "A User" doend ©Nascenia IT
  5. 5. describe + itspec/lib/user_spec.rbrequire spec_helperdescribe "A User" do it "is named Matsumoto"end ©Nascenia IT
  6. 6. pendingspec/lib/user_spec.rbrequire spec_helperdescribe "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:4Finished in 0.00026 seconds1 example, 0 failures, 1 pending ©Nascenia IT
  7. 7. describe classspec/lib/user_spec.rbrequire spec_helperdescribe User do it is named Matsumotoend$ rspec spec/lib/user_spec.rbuser_spec.rb:3:in `<top (required)>: uninitialized constant User(NameError) ©Nascenia IT
  8. 8. create classspec/lib/user_spec.rb lib/user.rbrequire spec_helper class Userdescribe User do #empty class it is named Matsumoto endend$ rspec spec/lib/user_spec.rb*Pending: User is named Matsumoto # Not yet implemented # ./spec/lib/user_spec.rb:5Finished in 0.00059 seconds1 example, 0 failures, 1 pending ©Nascenia IT
  9. 9. expectationsspec/lib/user_spec.rbrequire "spec_helper"require userdescribe User do it is named Matsumoto do user = User.new user.name.should == "Matsumoto" endend$ rspec spec/lib/user_spec.rbFFailures: 1) User is named Matsumoto Failure/Error: user.name.should == "Matsumoto“ NoMethodError: undefined method `name for #<User:0x0000000129e540>… ©Nascenia IT
  10. 10. success!spec/lib/user_spec.rb lib/user.rbrequire "spec_helper" class Userrequire user attr_accessor :namedescribe User do it is named Matsumoto do def initialize user = User.new @name = Matsumoto user.name.should == "Matsumoto" end endend end $ rspec spec/lib/user_spec.rb . Finished in 0.00074 seconds 1 example, 0 failures ©Nascenia IT
  11. 11. expectation (one more!)spec/lib/user_spec.rb lib/user.rbrequire "spec_helper" class Userrequire 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 = 1end end end$ rspec spec/lib/user_spec.rb..Finished in 0.00049 seconds2 examples, 0 failures ©Nascenia IT
  12. 12. more matchersuser.name.should == "Matsumoto"user.fit_for_drive.should == trueuser.fit_for_drive.should be_trueuser.height.should be < 7.5user.length.should_not >= 7.5 ©Nascenia IT
  13. 13. pendingit is named Matsumoto #It without bodyxit is named Matsumoto do failed codesendit is named Matsumoto do pending failed codesend ©Nascenia IT
  14. 14. with railsGemfilegroup :development, :test do gem rspec-railsend$ bundle install$ rails generate rspec:installcreate .rspeccreate speccreate spec/spec_helper.rb ©Nascenia IT
  15. 15. configurationspec/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 :rspecend ©Nascenia IT
  16. 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. 17. model specspec/models/user_spec.rbrequire spec_helperdescribe User do it "is not valid without name" do user = User.new user.should_not be_valid endend$ rspec spec/models/user_spec.rbFFailures: 1) User is not valid without name… ©Nascenia IT
  18. 18. model specspec/models/user_spec.rb app/models/user.rbrequire 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 endend$ rspec spec/models/user_spec.rb.Finished in 0.08148 seconds1 example, 0 failures ©Nascenia IT
  19. 19. hookspec/models/user_spec.rb spec/models/user_spec.rbdescribe 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 endend ©Nascenia IT
  20. 20. hook (Contd.)before(:each)before(:all)after(:each)after(:all) ©Nascenia IT
  21. 21. hook in contextspec/models/user_spec.rbdescribe 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 endend ©Nascenia IT
  22. 22. hook in contextspec/models/user_spec.rbdescribe 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 endend ©Nascenia IT
  23. 23. Custom matchersit { 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. 24. keep in touch!facebook.com/nascenia @NasceniaIT ©Nascenia IT
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×