Automated testing with RSpec
-
Upload
nascenia-it -
Category
Documents
-
view
2.483 -
download
4
description
Transcript of Automated testing with RSpec
automated testing with RSpec
Fattahul Alamwww.nascenia.com
• A popular ruby testing framework• Less Ruby-like, natural syntax• Well formatted output
©Nascenia IT
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
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
require 'spec_helper'
describe "A User" do
it "is named Matsumoto"
end
©Nascenia IT
spec/lib/user_spec.rb
describe + it
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
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
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
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
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!
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!)
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
it 'is named Matsumoto' #It without body
xit 'is named Matsumoto' dofailed codes
end
it 'is named Matsumoto' dopendingfailed codes
end
©Nascenia IT
pending
Gemfilegroup :development, :test do
gem 'rspec-rails'end
$ bundle install
$ rails generate rspec:installcreate .rspeccreate speccreate spec/spec_helper.rb
©Nascenia IT
with rails
...# 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
$ 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
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
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
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
before(:each)
before(:all)
after(:each)
after(:all)
©Nascenia IT
hook (Contd.)
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
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
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
facebook.com/nascenia@NasceniaIT
©Nascenia IT
keep in touch!