Module: Remarkable::ActionController::MacroStubs::ClassMethods
- Defined in:
- lib/remarkable_rails/action_controller/macro_stubs.rb
Instance Method Summary collapse
-
#describe(*args, &block) ⇒ Object
Overwrites describe to provide quick action description with I18n.
-
#expects(*args, &block) ⇒ Object
Creates a chain that will be evaluated as stub or expectation.
-
#mime(mime) ⇒ Object
The mime type of the request.
-
#mock_models(*models) ⇒ Object
(also: #mock_model)
Creates mock methods automatically.
-
#params(params) ⇒ Object
The params used for the request.
-
#run_callbacks_once!(&block) ⇒ Object
Undefine the method run_callbacks so rspec won’t run them in the before and after :each cycle.
-
#xhr!(bool = true) ⇒ Object
Sets the request to perform a XmlHttpRequest.
Instance Method Details
#describe(*args, &block) ⇒ Object
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
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 373 def describe(*args, &block) = args.first.is_a?(Hash) ? args.first : {} verb = (.keys & HTTP_VERBS_METHODS).first if verb action = .delete(verb) verb = verb.to_s description = Remarkable.t 'remarkable.action_controller.responding', :default => "responding to \#{{verb}} {{action}}", :verb => verb.sub('!', '').upcase, :action => action send_args = [ verb, action, ] elsif args.first.is_a?(Mime::Type) mime = args.first description = Remarkable.t 'remarkable.action_controller.mime_type', :default => "with #{mime.to_sym}", :format => mime.to_sym, :content_type => mime.to_s send_args = [ :mime, mime ] else # return if no special type was found return super(*args, &block) end args.shift args.unshift(description) # Creates an example group, send the method and eval the given block. # example_group = super(*args) do send(*send_args) instance_eval(&block) end end |
#expects(*args, &block) ⇒ Object
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
228 229 230 231 232 233 234 235 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 228 def expects(*args, &block) = args. .assert_valid_keys(:on, :with, :returns, :times, :ordered) args.each do |arg| write_inheritable_array(:expects_chain, [ [ arg, , block] ]) end end |
#mime(mime) ⇒ Object
The mime type of the request. The value given will be called transformed into a string and set in the @request.env variable.
Examples
mime Mime::XML
mime 'application/xml+rss'
245 246 247 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 245 def mime(mime) write_inheritable_attribute(:default_mime, mime.to_s) end |
#mock_models(*models) ⇒ Object Also known as: mock_model
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.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 448 def mock_models(*models) = models. = { :class_method => true }.merge() models.each do |model| model = model.to_s klass = [:as] || model.classify if [:class_method] (class << self; self; end).class_eval <<-METHOD def #{model}_proc; proc { mock_#{model} }; end def #{model.pluralize}_proc; proc { [ mock_#{model} ] }; end alias :mock_#{model} :#{model}_proc alias :mock_#{model.pluralize} :#{model.pluralize}_proc METHOD end self.class_eval <<-METHOD def mock_#{model}(stubs={}) @#{model} ||= mock_model(#{klass}, stubs) end METHOD end end |
#params(params) ⇒ Object
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
261 262 263 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 261 def params(params) write_inheritable_hash(:default_params, params) end |
#run_callbacks_once!(&block) ⇒ Object
Undefine the method run_callbacks so rspec won’t run them in the before and after :each cycle. Then we redefine it as run_callbacks_once, which will be used as an before(:all) and after(:all) filter.
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 317 def run_callbacks_once!(&block) #:nodoc: unless instance_methods.any?{|m| m.to_s == 'run_callbacks_once' } alias_method :run_callbacks_once, :run_callbacks class_eval "def run_callbacks(*args); end" before(:all) do setup_mocks_for_rspec run_callbacks_once :setup before_all_block.each do |block| instance_eval(&block) end if before_all_block run_action! verify_mocks_for_rspec teardown_mocks_for_rspec end after(:all) do run_callbacks_once :teardown end end end |
#xhr!(bool = true) ⇒ Object
Sets the request to perform a XmlHttpRequest.
Examples
describe TasksController do
xhr!
end
273 274 275 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 273 def xhr!(bool=true) write_inheritable_attribute(:default_xhr, bool) end |