Module Remarkable::ActionController::MacroStubs::ClassMethods
In: lib/remarkable_rails/action_controller/macro_stubs.rb

Methods

describe   expects   mime   mock_model   mock_models   params   xhr!  

Public Instance methods

Overwrites describe to provide quick action description with I18n.

You can now do:

  describe :get => :show, :id => 37

Which is the same as:

  describe 'responding to #GET show' do
    get :show, :id => 37

And do this:

  describe Mime::XML

Which is the same as:

  describe 'with xml' do
    mime Mime::XML

The string can be localized using I18n. An example yml file is:

  locale:
    remarkable:
      action_controller:
        responding: "responding to #{{verb}} {{action}}"
        mime_type: "with {{format}} ({{content_type}})"

And load the locale file with:

  Remarkable.add_locale locale_path

[Source]

     # File lib/remarkable_rails/action_controller/macro_stubs.rb, line 370
370:         def describe(*args, &block)
371:           options = args.first.is_a?(Hash) ? args.first : {}
372:           verb    = (options.keys & HTTP_VERBS_METHODS).first
373: 
374:           if verb
375:             action = options.delete(verb)
376:             verb   = verb.to_s
377: 
378:             description = Remarkable.t 'remarkable.action_controller.responding',
379:                                         :default => "responding to \#{{verb}} {{action}}",
380:                                         :verb => verb.sub('!', '').upcase, :action => action
381: 
382:             send_args = [ verb, action, options ]
383:           elsif args.first.is_a?(Mime::Type)
384:             mime = args.first
385: 
386:             description = Remarkable.t 'remarkable.action_controller.mime_type',
387:                                         :default => "with #{mime.to_sym}",
388:                                         :format => mime.to_sym, :content_type => mime.to_s
389: 
390:             send_args = [ :mime, mime ]
391:           else # return if no special type was found

392:             return super(*args, &block)
393:           end
394: 
395:           args.shift
396:           args.unshift(description)
397: 
398:           # Creates an example group, send the method and eval the given block.

399:           #

400:           example_group = super(*args) do
401:             send(*send_args)
402:             instance_eval(&block)
403:           end
404:         end

Creates a chain that will be evaluated as stub or expectation. The first parameter is the method expected. You can also specify multiple methods to stub and give a block to calculate the returned value. See examples below.

Options

  • :on - Tell which object will receive the expected method. This option is always required.
  • :with - Tell each parameters will be sent with the expected method. This option is used only in expectations and is optional.
  • :returns - Tell what the expectations should return. Not required.
  • :times - The number of times the object will receive the method. Used only in expectations and when not given, defaults to 1.
  • :ordered - When true specifies that expectations should be received in order.

Example

  expects :new, :on => Project, :returns => :project_proc, :times => 2

  expects :new, :find, :on => Project, :returns => :project_proc

  expects :human_attribute_name, :on => Project, :with => :title do |attr|
    attr.to_s.humanize
  end

[Source]

     # File lib/remarkable_rails/action_controller/macro_stubs.rb, line 223
223:         def expects(*args, &block)
224:           options = args.extract_options!
225:           options.assert_valid_keys(:on, :with, :returns, :times, :ordered)
226: 
227:           args.each do |arg|
228:             write_inheritable_array(:expects_chain, [ [ arg, options, block] ])
229:           end
230:         end

The mime type of the request. The value given will be called transformed into a string and set in the @request.env[‘HTTP_ACCEPT’] variable.

Examples

  mime Mime::XML
  mime 'application/xml+rss'

[Source]

     # File lib/remarkable_rails/action_controller/macro_stubs.rb, line 240
240:         def mime(mime)
241:           write_inheritable_attribute(:default_mime, mime.to_s)
242:         end
mock_model(*models)

Alias for mock_models

Creates mock methods automatically.

Options

  • :as - Used to set the model . For example, if you have Admin::Task model, you have to tell the name of the class to be mocked:
       mock_models :admin_task, :as => "Admin::Task"
    
  • :class_method - When set to false, does not create the class method which returns a proc.

Examples

Doing this:

  describe ProjectsController do
    mock_models :project
  end

Will create one instance and two class mock methods for you:

  def self.project_proc
    proc { mock_project }
  end

  # To be used on index actions
  def self.projects_procs
    proc { [ mock_project ] }
  end

  def mock_project(stubs={})
    @project ||= mock_model(Project, stubs)
  end

If you want to create just the instance method, you can give :class_method => false as option.

[Source]

     # File lib/remarkable_rails/action_controller/macro_stubs.rb, line 445
445:         def mock_models(*models)
446:           options = models.extract_options!
447:           options = { :class_method => true }.merge(options)
448: 
449:           models.each do |model|
450:             model = model.to_s
451:             klass = options[:as] || model.classify
452: 
453:             if options[:class_method]
454:               (class << self; self; end).class_eval "def \#{model}_proc; proc { mock_\#{model} }; end\ndef \#{model.pluralize}_proc; proc { [ mock_\#{model} ] }; end\n\nalias :mock_\#{model} :\#{model}_proc\nalias :mock_\#{model.pluralize} :\#{model.pluralize}_proc\n"
455:             end
456: 
457:             self.class_eval "def mock_\#{model}(stubs={})\n@\#{model} ||= mock_model(\#{klass}, stubs)\nend\n"
458:           end
459:         end

The params used for the request. Calls are always nested:

Examples

  describe TasksController do
    params :project_id => 42

    describe :get => :show, :id => 37 do
      # will request with params {:id => 37, :project_id => 42}
    end
  end

[Source]

     # File lib/remarkable_rails/action_controller/macro_stubs.rb, line 256
256:         def params(params)
257:           write_inheritable_hash(:default_params, params)
258:         end

Sets the request to perform a XmlHttpRequest.

Examples

  describe TasksController do
    xhr!
  end

[Source]

     # File lib/remarkable_rails/action_controller/macro_stubs.rb, line 268
268:         def xhr!(bool=true)
269:           write_inheritable_attribute(:default_xhr, bool)
270:         end

[Validate]