Module: ShouldaExt::Matchers

Included in:
Test::Unit::TestCase
Defined in:
lib/shoulda_ext/matchers.rb,
lib/shoulda_ext/matchers/trigger_callback.rb,
lib/shoulda_ext/matchers/respond_with_json.rb,
lib/shoulda_ext/matchers/record_count_change.rb

Overview

:nodoc:

Defined Under Namespace

Classes: RecordCountChangeMatcher, RespondWithJsonMatcher, TriggerCallbackMatcher

Constant Summary collapse

CALLBACK_EVENTS =
[:before, :after, :after_commit_on]
CALLBACK_TYPES =
[:create, :update, :destroy, :save]

Instance Method Summary collapse

Instance Method Details

#change_record_countObject

Tests the difference in record count before and after the current setup/subject block Can be used with the follow methods:

- for(klass)
  Provides the class which the test is being performed on.  Can be a constant or a symbol

- by(count)
  Provides an expected difference for the number of records for the specified class.  If 
  this count is not specified, the matcher will test for any difference.

Examples:

context "creating a blog article" do

  context "with good parameters" do
    setup do
      post :create, :blog => {:title => 'my blog post', :body => 'Ipsum lorem...'}
    end

    # All of the below are synonomous
    should create_record Blog
    should create_record :blog
    should change_record_count.for(Blog).by(1)
  end

  context "without a body" do
    setup do
      post :create, :blog => {:title => 'my blog post' }
    end

    # All of the below are synonomous
    should_not create_record Blog
    should_not change_record_count.for(Blog)
  end

end


66
67
68
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 66

def change_record_count
  RecordCountChangeMatcher.new
end

#create_record(klass) ⇒ Object

Test if a record of klass is created during the current setup/subject block Equivilent to change_record_count.for(klass).by(1)



9
10
11
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 9

def create_record(klass)
  RecordCountChangeMatcher.new.for(klass).by(1)
end

#create_records(klass, count) ⇒ Object

Test if ‘count’ records of ‘klass’ are created during the current setup/subject block Equivilent to change_record_count.for(klass).by(count)



21
22
23
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 21

def create_records(klass, count)
  RecordCountChangeMatcher.new.for(klass).by(count)
end

#destroy_record(klass) ⇒ Object

Test if a record of klass is destroyed during the current setup/subject block Equivilent to change_record_count.for(klass).by(-1)



15
16
17
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 15

def destroy_record(klass)
  RecordCountChangeMatcher.new.for(klass).by(-1)
end

#destroy_records(klass, count) ⇒ Object

Test if ‘count’ records of ‘klass’ are destroyed during the current setup/subject block

Equivilent to change_record_count.for(klass).by(-count)



27
28
29
# File 'lib/shoulda_ext/matchers/record_count_change.rb', line 27

def destroy_records(klass, count)
  RecordCountChangeMatcher.new.for(klass).by(-count)
end

#respond_with_json(description = nil, &block) ⇒ Object

Check if the controller’s response is json

Example uses:

context ":index.json" do
 setup do
   get :index, :format => 'json'
 end
 # Just check to see that the response was json
 should respond_with_json  

 # Evaluate the hash produced by the json yourself
 should respond_with_json { |json| json.first['blog']['title'] == 'blog post 1'}

 # Provide an exact match
 should respond_with_json.exactly(['blog' => {'id' => 1, 'title' => 'blog post 1'}])

 # Provide an exact match with a block
 should response_with_json.exactly{ |json| JSON.parse(Blog.all.to_json)}

end

context “:index.html” do

setup do
  get :index
end

# or the negation
should_not respond_with_json

end



32
33
34
# File 'lib/shoulda_ext/matchers/respond_with_json.rb', line 32

def respond_with_json(description = nil, &block)
  RespondWithJsonMatcher.new(self, description, &block)
end

#trigger_callbacksObject

Test if create, update, destroy, or save callbacks were triggered. Examples:

context "doing nothing to a record" do
  subject { Blog.new :title => 'blog title' }
  should_not trigger_callbacks
end

context "creating a record" do
  subject { Blog.create! :title => 'blog title' }
  should trigger_callbacks.for :create
  should_not trigger_callbacks.for :update, :destroy
end


21
22
23
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 21

def trigger_callbacks
  TriggerCallbackMatcher.new.any
end