Module: Remarkable::ActionController::Matchers

Defined in:
lib/remarkable_rails/action_controller/matchers/route_matcher.rb,
lib/remarkable_rails/action_controller/matchers/assign_to_matcher.rb,
lib/remarkable_rails/action_controller/matchers/redirect_to_matcher.rb,
lib/remarkable_rails/action_controller/matchers/set_cookies_matcher.rb,
lib/remarkable_rails/action_controller/matchers/set_session_matcher.rb,
lib/remarkable_rails/action_controller/matchers/respond_with_matcher.rb,
lib/remarkable_rails/action_controller/matchers/filter_params_matcher.rb,
lib/remarkable_rails/action_controller/matchers/set_the_flash_matcher.rb,
lib/remarkable_rails/action_controller/matchers/render_template_matcher.rb

Defined Under Namespace

Classes: AssignToMatcher, FilterParamsMatcher, RedirectToMatcher, RenderTemplateMatcher, RespondWithMatcher, RouteMatcher, SetCookiesMatcher, SetSessionMatcher, SetTheFlashMatcher

Instance Method Summary collapse

Instance Method Details

#assign_to(*args, &block) ⇒ Object

Checks if the controller assigned the variables given by name. If you want to check that a variable is not being assigned, please do:

should_not_assign_to(:user)

If you want to assure that a variable is being assigned to nil, do instead:

should_assign_to(:user).with(nil)

Options

  • :with - The value to compare the assign. It can be also be supplied as proc or as a block (see examples below)

  • :with_kind_of - The expected class of the assign.

Examples

should_assign_to :user, :with_kind_of => User
should_assign_to :user, :with => proc{ users(:first) }
should_assign_to(:user){ users(:first) }

it { should assign_to(:user) }
it { should assign_to(:user, :with => users(:first)) }
it { should assign_to(:user, :with_kind_of => User) }


88
89
90
# File 'lib/remarkable_rails/action_controller/matchers/assign_to_matcher.rb', line 88

def assign_to(*args, &block)
  AssignToMatcher.new(*args, &block).spec(self)
end

#filter_params(*params, &block) ⇒ Object Also known as: filter_param

Checks if the controller filters the given params.

Examples

should_filter_params :password
should_not_filter_params :username

it { should filter_params(:password) }
it { should_not filter_params(:username) }


34
35
36
# File 'lib/remarkable_rails/action_controller/matchers/filter_params_matcher.rb', line 34

def filter_params(*params, &block)
  FilterParamsMatcher.new(*params, &block).spec(self)
end

#redirect_to(expected = nil, options = {}, &block) ⇒ Object

Passes if the response redirects to the given url. The url can be a string, a hash or can be supplied as a block (see examples below).

Options

  • :with - The status 30X used when redirecting.

Examples

should_redirect_to{ users_url }
should_redirect_to(:action => 'index')
should_not_redirect_to(:controller => 'users', :action => 'new')

it { should redirect_to(users_url).with(302) }
it { should redirect_to(:action => 'index') }
it { should_not redirect_to(:controller => 'users', :action => 'new') }


113
114
115
# File 'lib/remarkable_rails/action_controller/matchers/redirect_to_matcher.rb', line 113

def redirect_to(expected=nil, options={}, &block)
  RedirectToMatcher.new(expected, options, &block).spec(self)
end

#render_template(*args, &block) ⇒ Object

Passes if the specified template (view file) is rendered by the response. This file can be any view file, including a partial.

template can include the controller path. It can also include an optional extension, which you only need to use when there is ambiguity.

Note that partials must be spelled with the preceding underscore.

Options

  • :layout - The layout used when rendering the template.

All other options in respond_with are also available.

Examples

should_render_template 'list'
should_render_template 'same_controller/list'
should_render_template 'other_controller/list'

# with extensions

should_render_template 'list.rjs'
should_render_template 'list.haml'
should_render_template 'same_controller/list.rjs'
should_render_template 'other_controller/list.rjs'

# partials

should_render_template '_a_partial'
should_render_template 'same_controller/_a_partial'
should_render_template 'other_controller/_a_partial'

# with options

should_render_template 'list', :layout => 'users'
should_render_template 'list', :content_type => :xml
should_render_template 'list', :content_type => /xml/
should_render_template 'list', :content_type => Mime::XML

it { should render_template('list').layout('users') }
it { should render_template('list').content_type(:xml) }
it { should render_template('list').content_type(/xml/) }
it { should render_template('list').content_type(Mime::XML) }

Gotcha

Extensions check does not work in Rails 2.1.x.



124
125
126
127
128
# File 'lib/remarkable_rails/action_controller/matchers/render_template_matcher.rb', line 124

def render_template(*args, &block)
  options = args.extract_options!
  options.merge!(:template => args.first)
  RenderTemplateMatcher.new(options, &block).spec(self)
end

#render_with_layout(*args, &block) ⇒ Object

This is just a shortcut for render_template :layout => layout. It’s also used for Shoulda compatibility. Check render_template for more information.



133
134
135
136
137
# File 'lib/remarkable_rails/action_controller/matchers/render_template_matcher.rb', line 133

def render_with_layout(*args, &block)
  options = args.extract_options!
  options.merge!(:layout => args.first)
  RenderTemplateMatcher.new(options, &block).spec(self)
end

#render_without_layout(options = {}, &block) ⇒ Object

This is just a shortcut for render_template :layout => nil. It’s also used for Shoulda compatibility. Check render_template for more information.



142
143
144
145
# File 'lib/remarkable_rails/action_controller/matchers/render_template_matcher.rb', line 142

def render_without_layout(options={}, &block)
  options.merge!(:layout => nil)
  RenderTemplateMatcher.new(options, &block).spec(self)
end

#respond_with(*args, &block) ⇒ Object

Passes if the response has the given status. Status can be a Symbol lik :success, :missing, :redirect and :error. Can be also a Fixnum, Range o any other symbol which matches to any of Rails status codes.

Options

  • :body - The body of the response. It accepts strings and or regular expressions. Altought you might be running your tests without integrating your views, this is useful when rendering :xml or :text.

  • :content_type - The content type of the response. It accepts strings (‘application/rss+xml’), mime constants (Mime::RSS), symbols (:rss) and regular expressions /rss/.

Examples

should_respond_with :success
should_respond_with :error,   :body => /System error/
should_respond_with 301,      :content_type => Mime::XML
should_respond_with 300..399, :content_type => Mime::XML

it { should respond_with(:success)                              }
it { should respond_with(:error).body(/System error/)           }
it { should respond_with(301).content_type(Mime::XML)      }
it { should respond_with(300..399).content_type(Mime::XML) }


107
108
109
110
111
# File 'lib/remarkable_rails/action_controller/matchers/respond_with_matcher.rb', line 107

def respond_with(*args, &block)
  options = args.extract_options!
  options.merge!(:with => args.first)
  RespondWithMatcher.new(options, &block).spec(self)
end

#respond_with_body(*args, &block) ⇒ Object

This is just a shortcut for respond_with :body => body. Check respond_with for more information.



116
117
118
119
120
121
122
# File 'lib/remarkable_rails/action_controller/matchers/respond_with_matcher.rb', line 116

def respond_with_body(*args, &block)
  options = args.extract_options!
  # Since body can be also given as block, only merge if any arguments was
  # actually sent.
  options.merge!(:body => args.first) unless args.empty?
  RespondWithMatcher.new(options, &block).spec(self)
end

#respond_with_content_type(*args, &block) ⇒ Object

This is just a shortcut for respond_with :content_type => content_type. It’s also used for Shoulda compatibility. Check respond_with for more information.



128
129
130
131
132
# File 'lib/remarkable_rails/action_controller/matchers/respond_with_matcher.rb', line 128

def respond_with_content_type(*args, &block)
  options = args.extract_options!
  options.merge!(:content_type => args.first)
  RespondWithMatcher.new(options, &block).spec(self)
end

#route(*params, &block) ⇒ Object

Assert route generation AND route recognition.

Examples

# autodetects the :controller
should_route :get,    '/posts',         :action => :index

# explicitly specify :controller
should_route :post,   '/posts',         :controller => :posts, :action => :create

# non-string parameter
should_route :get,    '/posts/1',       :controller => :posts, :action => :show,    :id => 1

# string-parameter
should_route :put,    '/posts/1',       :controller => :posts, :action => :update,  :id => "1"
should_route :delete, '/posts/1',       :controller => :posts, :action => :destroy, :id => 1
should_route :get,    '/posts/new',     :controller => :posts, :action => :new

# nested routes
should_route :get,    '/users/5/posts', :controller => :posts, :action => :index,   :user_id => 5
should_route :post,   '/users/5/posts', :controller => :posts, :action => :create,  :user_id => 5

# it example
it { should route(:get, :action => :index).to('/users/5/posts') }


129
130
131
# File 'lib/remarkable_rails/action_controller/matchers/route_matcher.rb', line 129

def route(*params, &block)
  RouteMatcher.new(*params, &block).spec(self)
end

#set_cookies(*args, &block) ⇒ Object Also known as: set_cookie

Ensures that the given cookie keys were set. If you want to check that a cookie is not being set, just do:

should_not_set_cookies :user

If you want to assure that a cookie is being set to nil, do instead:

should_set_cookies :user, :to => nil

Note: this method is also aliased as set_cookie.

Options

  • :to - The value to compare the session key. It accepts procs and be also given as a block (see examples below).

Examples

should_set_cookies :user_id, :user
should_set_cookies :user_id, :to => 2
should_set_cookies :user, :to => proc{ users(:first) }
should_set_cookies(:user){ users(:first) }

it { should set_cookies(:user_id, :user) }
it { should set_cookies(:user_id, :to => 2) }
it { should set_cookies(:user, :to => users(:first)) }


78
79
80
# File 'lib/remarkable_rails/action_controller/matchers/set_cookies_matcher.rb', line 78

def set_cookies(*args, &block)
  SetCookiesMatcher.new(*args, &block).spec(self)
end

#set_session(*args, &block) ⇒ Object

Ensures that the given session keys were set. If you want to check that a variable is not being set, just do:

should_not_set_session :user

If you want to assure that a variable is being set to nil, do instead:

should_set_session :user, :to => nil

Options

  • :to - The value to compare the session key. It accepts procs and be also given as a block (see examples below).

Examples

should_set_session :user_id, :user
should_set_session :user_id, :to => 2
should_set_session :user, :to => proc{ users(:first) }
should_set_session(:user){ users(:first) }

it { should set_session(:user_id, :user) }
it { should set_session(:user_id, :to => 2) }
it { should set_session(:user, :to => users(:first)) }


102
103
104
# File 'lib/remarkable_rails/action_controller/matchers/set_session_matcher.rb', line 102

def set_session(*args, &block)
  SetSessionMatcher.new(*args, &block).spec(self)
end

#set_the_flash(*args, &block) ⇒ Object

Ensures that a flash message is being set. If you want to check that a flash is not being set, just do:

should_not_set_the_flash :user

If you want to assure that a flash is being set to nil, do instead:

should_set_the_flash :user, :to => nil

Options

  • :to - The value to compare the flash key. It accepts procs and can also be given as a block (see examples below)

Examples

should_set_the_flash
should_not_set_the_flash

should_set_the_flash :to => 'message'
should_set_the_flash :notice, :warn
should_set_the_flash :notice, :to => 'message'
should_set_the_flash :notice, :to => proc{ 'hi ' + users(:first).name }
should_set_the_flash(:notice){ 'hi ' + users(:first).name }

it { should set_the_flash }
it { should set_the_flash.to('message') }
it { should set_the_flash(:notice, :warn) }
it { should set_the_flash(:notice, :to => 'message') }
it { should set_the_flash(:notice, :to => ('hi ' + users(:first).name)) }


50
51
52
# File 'lib/remarkable_rails/action_controller/matchers/set_the_flash_matcher.rb', line 50

def set_the_flash(*args, &block)
  SetTheFlashMatcher.new(*args, &block).spec(self)
end