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
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
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 408 409 410 411 412 413 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 379 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 => :mock_project, :times => 2
expects :new, :find, :on => Project, :returns => :mock_project
expects :human_attribute_name, :on => Project, :with => :title do |attr|
attr.to_s.humanize
end
234 235 236 237 238 239 240 241 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 234 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'
251 252 253 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 251 def mime(mime) write_inheritable_attribute(:default_mime, mime.to_s) end |
#mock_models(*models) ⇒ Object
Creates mock methods automatically.
Options
-
: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.mock_project
proc { mock_project }
end
# To be used on index actions
def self.mock_projects
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 |
# 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 self.class_eval <<-METHOD #{"def self.mock_#{model}; proc { mock_#{model} }; end" if [:class_method]} #{"def self.mock_#{model.pluralize}; proc { [ mock_#{model} ] }; end" if [:class_method]} def mock_#{model}(stubs={}) @#{model} ||= mock_model(#{model.classify}, 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
267 268 269 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 267 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.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 323 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
279 280 281 |
# File 'lib/remarkable_rails/action_controller/macro_stubs.rb', line 279 def xhr!(bool=true) write_inheritable_attribute(:default_xhr, bool) end |