Module Remarkable::ActionController::Matchers
In: lib/remarkable_rails/action_controller/matchers/route_matcher.rb
lib/remarkable_rails/action_controller/matchers/set_the_flash_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/assign_to_matcher.rb
lib/remarkable_rails/action_controller/matchers/filter_params_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/render_template_matcher.rb

Methods

Public Instance methods

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) }

[Source]

    # File lib/remarkable_rails/action_controller/matchers/assign_to_matcher.rb, line 88
88:       def assign_to(*args, &block)
89:         AssignToMatcher.new(*args, &block).spec(self)
90:       end
filter_param(*params, &block)

Alias for filter_params

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) }

[Source]

    # File lib/remarkable_rails/action_controller/matchers/filter_params_matcher.rb, line 34
34:       def filter_params(*params, &block)
35:         FilterParamsMatcher.new(*params, &block).spec(self)
36:       end

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') }

[Source]

     # File lib/remarkable_rails/action_controller/matchers/redirect_to_matcher.rb, line 113
113:       def redirect_to(expected=nil, options={}, &block)
114:         RedirectToMatcher.new(expected, options, &block).spec(self)
115:       end

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.

[Source]

     # File lib/remarkable_rails/action_controller/matchers/render_template_matcher.rb, line 124
124:       def render_template(*args, &block)
125:         options = args.extract_options!
126:         options.merge!(:template => args.first)
127:         RenderTemplateMatcher.new(options, &block).spec(self)
128:       end

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

[Source]

     # File lib/remarkable_rails/action_controller/matchers/render_template_matcher.rb, line 133
133:       def render_with_layout(*args, &block)
134:         options = args.extract_options!
135:         options.merge!(:layout => args.first)
136:         RenderTemplateMatcher.new(options, &block).spec(self)
137:       end

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

[Source]

     # File lib/remarkable_rails/action_controller/matchers/render_template_matcher.rb, line 142
142:       def render_without_layout(options={}, &block)
143:         options.merge!(:layout => nil)
144:         RenderTemplateMatcher.new(options, &block).spec(self)
145:       end

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) }

[Source]

     # File lib/remarkable_rails/action_controller/matchers/respond_with_matcher.rb, line 107
107:       def respond_with(*args, &block)
108:         options = args.extract_options!
109:         options.merge!(:with => args.first)
110:         RespondWithMatcher.new(options, &block).spec(self)
111:       end

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

[Source]

     # File lib/remarkable_rails/action_controller/matchers/respond_with_matcher.rb, line 116
116:       def respond_with_body(*args, &block)
117:         options = args.extract_options!
118:         # Since body can be also given as block, only merge if any arguments was
119:         # actually sent.
120:         options.merge!(:body => args.first) unless args.empty?
121:         RespondWithMatcher.new(options, &block).spec(self)
122:       end

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.

[Source]

     # File lib/remarkable_rails/action_controller/matchers/respond_with_matcher.rb, line 128
128:       def respond_with_content_type(*args, &block)
129:         options = args.extract_options!
130:         options.merge!(:content_type => args.first)
131:         RespondWithMatcher.new(options, &block).spec(self)
132:       end

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

[Source]

     # File lib/remarkable_rails/action_controller/matchers/route_matcher.rb, line 103
103:       def route(*params, &block)
104:         RouteMatcher.new(*params, &block).spec(self)
105:       end
set_cookie(*args, &block)

Alias for set_cookies

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)) }

[Source]

    # File lib/remarkable_rails/action_controller/matchers/set_cookies_matcher.rb, line 78
78:       def set_cookies(*args, &block)
79:         SetCookiesMatcher.new(*args, &block).spec(self)
80:       end

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)) }

[Source]

     # File lib/remarkable_rails/action_controller/matchers/set_session_matcher.rb, line 102
102:       def set_session(*args, &block)
103:         SetSessionMatcher.new(*args, &block).spec(self)
104:       end

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)) }

[Source]

    # File lib/remarkable_rails/action_controller/matchers/set_the_flash_matcher.rb, line 50
50:       def set_the_flash(*args, &block)
51:         SetTheFlashMatcher.new(*args, &block).spec(self)
52:       end

[Validate]