rspec1
TRANSCRIPT
-
7/29/2019 RSpec1
1/38
1
RSpec IRSpec I
Sang ShinSang Shinhttp://www.JPassion.comhttp://www.JPassion.com
Learn with JPassion!Learn with JPassion!
1
-
7/29/2019 RSpec1
2/38
2
RSpec I: Topics
What is and Why RSpec?
Installing and running RSpec
RSpec vocabulary
Subject Model testing with RSpec
RSpec tools Spork, Guard
The following topics will be covered in RSpec II.
Controller testing, View testing, more RSpec tools (Factory Girl,Faker, Capybara, Launchy)
-
7/29/2019 RSpec1
3/38
What is & Why RSpec?What is & Why RSpec?
-
7/29/2019 RSpec1
4/38
4
What is RSpec?
Ruby DSL (Domain Specific Language) for specifying thedesired behavior of Ruby code
Testing tool for the Ruby programming language
Born under the banner of BDD (Behaviour-Driven Development) Make TDD(Test-Driven Development) a productive and
enjoyable experience
Used to create unit, functional and integration testing
-
7/29/2019 RSpec1
5/38
5
Steps for BDD Testing using RSpec
Repeat the following steps
> Step #1: Write behavior of some test target as a set of specexamples
> Step #2: Run the test and experience test failures
> Step #3: Write code (or refactor code) to correct the test failures
-
7/29/2019 RSpec1
6/38
6
Why RSpec?
RSpec scripts (simply called specs) enables building readabletesting logic
> Descriptions attached to the blocks of code form readablesentences, which describe the desired behavior
> Documentation, testing, code are very much aligned A better and more popular alternative to default Rails test
system
-
7/29/2019 RSpec1
7/387
Displaying Bahavior/Examples
Display all examples in readable format
-
7/29/2019 RSpec1
8/388
RSpec Features
a rich command line program (the rspec command)
textual descriptions of examples and groups (rspec-core)
flexible and customizable reporting
extensible expectation language (rspec-expectations) built-in mocking/stubbing framework (rspec-mocks)
-
7/29/2019 RSpec1
9/38
Installing of &Installing of &Running RSpecRunning RSpec
-
7/29/2019 RSpec1
10/3810
RSpec Installation
Add rspec-rails gem in Gemfile and perform bundle install
group :development, :test do gem 'rspec-rails', "~> 2.0.1"end
Generate RSpec configuration filesrails generate rspec:install
-
7/29/2019 RSpec1
11/3811
Running RSpec (using rspec)
Run all specs
rspec spec
Run all model specs
rspec spec/models
Run a particular model spec
rspec spec/models/student_spec.rb
Run a particular model spec with 'documentation format
rspec spec/models/student_spec.rb --format documentation
-
7/29/2019 RSpec1
12/3812
Running RSpec (using rake)
You can also use rake to run RSpec
C:\rails_projects\rspec_app>rake -T spec
rake spec # Run all specs in spec directory (excluding plugin ...
rake spec:controllers # Run the code examples in spec/controllers
rake spec:helpers # Run the code examples in spec/helpersrake spec:lib # Run the code examples in spec/lib
rake spec:mailers # Run the code examples in spec/mailers
rake spec:models # Run the code examples in spec/models
rake spec:rcov # Run all specs with rcovrake spec:requests # Run the code examples in spec/requests
rake spec:routing # Run the code examples in spec/routing
rake spec:views # Run the code examples in spec/views
-
7/29/2019 RSpec1
13/3813
Demo:Demo:
Exercise 1: Configure RSpec toExercise 1: Configure RSpec toa Rails Applicationa Rails Application
5541_rails_rspec1.zip5541_rails_rspec1.zip
-
7/29/2019 RSpec1
14/38
RSpec VocabularyRSpec Vocabulary
-
7/29/2019 RSpec1
15/3815
Test::Unit vs RSpec
class SomethingTest
def test_talk
assert_xxx
def setup def teardown
describe Something
it should talk
object.should be_xxx
before(:each) { }, before(:all) {} after(:each) { }, after(:all) { }
pending
let
expect
context
specify
-
7/29/2019 RSpec1
16/3816
RSpec Verification Methods
RSpec provides two verification methods that are available for allobjects
> should> should_not
Examples> aObject.should respond_to(:attribute)> aObject.should be_valid> aObject.should be_nil> aObject.done?.should be_true> {:a => 'b'}.should have_key(:a), {:a => 'b'}.should_not have_key(:x)> 'a string'.should be_an_instance_of(String)> result.should equal(5)> message.should match(/hello/), message.should =~ /hello/
-
7/29/2019 RSpec1
17/3817
Spec Example
describe defines a behavior, which is a collection of examples
it defines an example
require 'spec_helper'
describe Student dobefore { @student = Student.new }
it "should have name field" do
@student.should respond_to(:name)endit "should have age field" do
@student.should respond_to(:age)end
end
-
7/29/2019 RSpec1
18/3818
Pending tests
Used to add testing examples without testing logic
describe Student doit "should have age field" do
pending to be implemented laterend
# If it method does not have do..end code block, it is# considered as a pending testit "should have name field"
end
-
7/29/2019 RSpec1
19/3819
describe & context
describe method is used to group together related(specification) examples of behavior
> describe method returns Behaviorobject
describe method takes two arguments
> String description (or Model for Model spec)> do... end code block that contains a set of it methods
Can be nested
> The code block of outer describe can contain sets of innerdescribe's
context is an alias for describe
> Use describe for things and use context for context
-
7/29/2019 RSpec1
20/38
20
it
it defines (specification) example
it method takes description and a do.. end code block (similarto describe method)
Verification always occur within the context of it block
Limit one expectation to one it block
-
7/29/2019 RSpec1
21/38
21
before & after
beforeand after methods executes code before and after> :each - executes code before each test> :all executes code once for all tests
describe Student dobefore(:each) { @student = Student.new }
it "should have name field" [email protected] respond_to(:name)
endit "should have age field" do
@student.should respond_to(:age)endit "should have email field" do
@student.should respond_to(:email)end
end
-
7/29/2019 RSpec1
22/38
22
let
let method executes code once and stored it for furtherinvocation, increasing the performance
describe Student do#before { @student = Student.new }
let (:student) { Student.new }
it "should have name field" do#@student.should respond_to(:name)
student.should respond_to(:name)end
it "should have age field" do#@student.should respond_to(:age) student.should respond_to(:age)
endit "should have email field" do
#@student.should respond_to(:email) student.should respond_to(:email)
endend
-
7/29/2019 RSpec1
23/38
23
expect
Used when a method is expected to change a value or throw anan exception
describe Student do
it "should increase the number of instances by 1 when new instancegets created" do expect {
Student.create}.to change {Student.count}.by(1)# }.to change {Student.count}.to(1)
# }.to change {Student.count}.from(0).to(1)end
end
-
7/29/2019 RSpec1
24/38
SubjectSubject
-
7/29/2019 RSpec1
25/38
25
What is a Subject?
Every RSpec example group represented by describe - hasa subject
A subject represents the thing that is being described
Could be implicit subject or explicit subject
-
7/29/2019 RSpec1
26/38
26
Implicit Subject
An implicit subject gets created automatically when the firstargument to the "describe" is a Model class
The implicit object can be referenced as "subject".
require 'spec_helper'
describe Student do
it "should have name field" do subject.should respond_to(:name)endit "should have age field" do
subject.should respond_to(:age)
endend
-
7/29/2019 RSpec1
27/38
27
Explicit Subject
You can create an subject explicitly for example, when youneed to create a subject with non-default property values
require 'spec_helper'
describe Student dobefore { @student = Student.new(:name =>Shin) }
it "should have name field" do
@student.should respond_to(:name)endit "should have age field" do
@student.should respond_to(:age)end
end
-
7/29/2019 RSpec1
28/38
28
Demo:Demo:
Exercise 2: Learn RSpec vocabularyExercise 2: Learn RSpec vocabulary5541_rails_rspec1.zip5541_rails_rspec1.zip
-
7/29/2019 RSpec1
29/38
Model TestingModel Testing
with RSpecwith RSpec
-
7/29/2019 RSpec1
30/38
30
Models: What to test
Attribute presence
Validation
Associations
Instance or class methods Custom finders
Callbacks
Scopes
-
7/29/2019 RSpec1
31/38
31
Attribute Presence Test
Studenthas name and age attributes
require 'spec_helper'
describe Student dobefore (:each) { @student = Student.new }
it "should have name field" [email protected]_to(:name)
endit "should have age field" do
@student.shouldrespond_to(:age)end
end
-
7/29/2019 RSpec1
32/38
32
Validation Test
Example validation tests
describe Student doit 'is not valid when name is null' do
@student = Student.new
@student.should_not be_validendit 'is not valid when the length of name is less than 3' do
@student = Student.new(:name => 'ab')@student.should_not be_valid
endit 'is not valid when age is not numerical value' do
@student = Student.new(:name =>'Sang', :age => 'abc')@student.should_not be_valid
endit 'is not valid when age is between 5 and 95' do
@student = Student.new(:name =>'Sang', :age => 3)@student.should_not be_valid
endend
-
7/29/2019 RSpec1
33/38
33
Association Test
Are association methods present?
Does the association refers to a correct object?describe Book do
it "belongs to a student" do@book = [email protected] respond_to(:student)
end
it "should have the right associated student" do@student = Student.create(:name => "Student1", :age => 30)@book = @student.books.create(:title => "JPassion", :author => "Sang Shin")
@book.student_id.should == @[email protected] == @studentend
end
-
7/29/2019 RSpec1
34/38
34
Demo:Demo:
Exercise 3: Perform Model Spec TestingExercise 3: Perform Model Spec Testing5541_rails_rspec1.zip5541_rails_rspec1.zip
-
7/29/2019 RSpec1
35/38
RSpec ToolsRSpec Tools
-
7/29/2019 RSpec1
36/38
36
RSpec Tools
Spork> Everytime RSpec testing is performed, a new Rails environment
needs to be created, which takes time> Spork loads Rails environment just once and all subsequent
RSpec uses it, thus saving time for Rails environment creation Guard
> Guard launch specs automatically & intelligently when files aremodified.
-
7/29/2019 RSpec1
37/38
37
Demo:Demo:
Exercise 4: Use SporkExercise 4: Use SporkExercise 5: Use GuardExercise 5: Use Guard
5541_rails_rspec1.zip5541_rails_rspec1.zip
-
7/29/2019 RSpec1
38/38
Thank you!Thank you!
Check JavaPassion.com Codecamps!Check JavaPassion.com Codecamps!http://www.javapassion.com/codecampshttp://www.javapassion.com/codecamps
http://www.javapassion.com/codecampshttp://www.javapassion.com/codecampshttp://www.javapassion.com/codecamps