Skip to content

Commit

Permalink
mock_models :foo now creates :foo_proc and :foos_proc methods, not :m…
Browse files Browse the repository at this point in the history
…ock_foo and :mock_foos (compatibility was maintained, but all docs were updated).
  • Loading branch information
josevalim committed Jun 4, 2009
1 parent 456d8f2 commit c601038
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
7 changes: 5 additions & 2 deletions remarkable_rails/CHANGELOG
@@ -1,3 +1,6 @@
* [DEPRECATION] mock_models now creates model_proc instead of mock_model.
Altough this won't fire any deprecation warning, all the documentation was changed.

* assert_valid_keys on expects

* mock_models now creates a second class method to be used on the index action [#71]
Expand Down Expand Up @@ -70,10 +73,10 @@ specs for a create action rewritten like this:
mock_models :task

describe :post => :create, :task => { :these => 'params' } do
expects :new, :on => Task, with => {'these' => 'params'}, :returns => mock_task
expects :new, :on => Task, with => {'these' => 'params'}, :returns => task_proc
expects :save, :on => mock_task, :returns => true

should_assign_to :task, :with => mock_task
should_assign_to :task, :with => task_proc
should_redirect_to { task_url(mock_task) }
end
end
Expand Down
8 changes: 4 additions & 4 deletions remarkable_rails/README
Expand Up @@ -65,11 +65,11 @@ An equivalent in remarkable would be:
mock_models :task

describe :post => :create, :task => { :these => 'params' } do
expects :new, :on => Task, :with => {'these' => 'params'}, :returns => mock_task
expects :save, :on => mock_task, :returns => true
expects :new, :on => Task, :with => {'these' => 'params'}, :returns => task_proc
expects :save, :on => task_proc, :returns => true

should_assign_to :task, :with => mock_task
should_redirect_to { task_url(mock_task) }
should_assign_to :task, :with => task_proc
should_redirect_to { task_url(task_proc) }
end
end

Expand Down
Expand Up @@ -84,12 +84,12 @@ module ActionController
#
# And it creates:
#
# def self.mock_project
# def self.project_proc
# proc { mock_project }
# end
#
# # To be used on index actions
# def self.mock_projects
# def self.projects_proc
# proc { [mock_project] }
# end
#
Expand All @@ -104,8 +104,8 @@ module ActionController
#
# For:
#
# expects :find, :on => Project, :with => '37', :returns => mock_project
# should_assign_to :project, :with => mock_project
# expects :find, :on => Project, :with => '37', :returns => project_proc
# should_assign_to :project, :with => project_proc
#
# = Give me more!
#
Expand All @@ -122,11 +122,11 @@ module ActionController
# params :project_id => '42' #=> define params for all requests
#
# # Those two expectations get inherited in all describe groups below
# expects :find_by_title, :on => Project, :with => '42', :returns => mock_project
# expects :find_by_title, :on => Project, :with => '42', :returns => project_proc
# expects :tasks, :and_return => Task
#
# describe :get => :show, :id => '37' do
# expects :find, :with => '37', :and_return => mock_task
# expects :find, :with => '37', :and_return => task_proc
#
# should_assign_to :project, :task
# should_render_template 'show'
Expand All @@ -140,7 +140,7 @@ module ActionController
# expectations with run_action!, run_expectations! and run_stubs!. Examples:
#
# describe :get => :new do
# expects :new, :on => Project, :returns => mock_project
# expects :new, :on => Project, :returns => project_proc
#
# it "should do something different" do
# run_action!
Expand Down Expand Up @@ -223,9 +223,9 @@ module ClassMethods
#
# == Example
#
# expects :new, :on => Project, :returns => :mock_project, :times => 2
# expects :new, :on => Project, :returns => :project_proc, :times => 2
#
# expects :new, :find, :on => Project, :returns => :mock_project
# expects :new, :find, :on => Project, :returns => :project_proc
#
# expects :human_attribute_name, :on => Project, :with => :title do |attr|
# attr.to_s.humanize
Expand Down Expand Up @@ -429,12 +429,12 @@ def describe(*args, &block)
#
# Will create one instance and two class mock methods for you:
#
# def self.mock_project
# def self.project_proc
# proc { mock_project }
# end
#
# # To be used on index actions
# def self.mock_projects
# def self.projects_procs
# proc { [ mock_project ] }
# end
#
Expand All @@ -451,10 +451,17 @@ def mock_models(*models)

models.each do |model|
model = model.to_s
self.class_eval <<-METHOD
#{"def self.mock_#{model}; proc { mock_#{model} }; end" if options[:class_method]}
#{"def self.mock_#{model.pluralize}; proc { [ mock_#{model} ] }; end" if options[:class_method]}
if options[: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(#{model.classify}, stubs)
end
Expand Down
40 changes: 24 additions & 16 deletions remarkable_rails/spec/action_controller/macro_stubs_spec.rb
Expand Up @@ -11,21 +11,29 @@ def current_id; '37'; end

describe 'mock_models' do
before(:each) do
self.class.metaclass.send(:undef_method, :mock_projects) if self.class.respond_to?(:mock_projects)
self.class.metaclass.send(:undef_method, :mock_project) if self.class.respond_to?(:mock_project)
self.class.metaclass.send(:undef_method, :projects_proc) if self.class.respond_to?(:projects_proc)
self.class.metaclass.send(:undef_method, :project_proc) if self.class.respond_to?(:project_proc)
self.class.send(:undef_method, :mock_project) if self.respond_to?(:mock_project)
end

it 'should create a class singular mock method' do
it 'should alias model_proc to mock_model' do
self.class.respond_to?(:mock_project).should be_false
self.class.respond_to?(:mock_projects).should be_false
self.class.mock_models :project
self.class.respond_to?(:mock_project).should be_true
self.class.respond_to?(:mock_projects).should be_true
end

it 'should create a class singular mock method' do
self.class.respond_to?(:project_proc).should be_false
self.class.mock_models :project
self.class.respond_to?(:project_proc).should be_true
end

it 'should create a class plural mock method' do
self.class.respond_to?(:mock_projects).should be_false
self.class.respond_to?(:projects_proc).should be_false
self.class.mock_models :project
self.class.respond_to?(:mock_projects).should be_true
self.class.respond_to?(:projects_proc).should be_true
end

it 'should create an instance mock method' do
Expand All @@ -35,15 +43,15 @@ def current_id; '37'; end
end

it 'should create just an instance method when :class_method is false' do
self.class.respond_to?(:mock_project).should be_false
self.class.respond_to?(:project_proc).should be_false
self.respond_to?(:mock_project).should be_false
self.class.mock_models :project, :class_method => false
self.class.respond_to?(:mock_project).should be_false
self.class.respond_to?(:project_proc).should be_false
self.respond_to?(:mock_project).should be_true
end

it 'should create procs which evals to a mock dynamically' do
proc = self.class.mock_task
proc = self.class.task_proc
proc.should be_kind_of(Proc)

@task.should be_nil
Expand All @@ -52,7 +60,7 @@ def current_id; '37'; end
end

it 'should create procs which evals to an array of mocks dynamically' do
proc = self.class.mock_tasks
proc = self.class.tasks_proc
proc.should be_kind_of(Proc)

@task.should be_nil
Expand All @@ -62,7 +70,7 @@ def current_id; '37'; end
end

describe 'failures' do
expects :find, :on => Task, :with => proc{ current_id }, :returns => mock_task
expects :find, :on => Task, :with => proc{ current_id }, :returns => task_proc
expects :max, :min, :count, :on => Task, :ordered => true

get :show, :id => 37
Expand Down Expand Up @@ -109,7 +117,7 @@ def current_id; '37'; end
end

describe 'when extending describe group behavior' do
expects :find, :on => Task, :with => proc{ current_id }, :returns => mock_task
expects :find, :on => Task, :with => proc{ current_id }, :returns => task_proc
expects :count, :max, :min, :on => Task

get :show, :id => 37
Expand Down Expand Up @@ -176,7 +184,7 @@ def current_id; '37'; end
end

describe Mime::XML do
expects :to_xml, :on => mock_task, :returns => 'XML'
expects :to_xml, :on => task_proc, :returns => 'XML'

it 'should provide a description based on the mime given in describe' do
self.class.description.should =~ /with xml$/
Expand Down Expand Up @@ -225,9 +233,9 @@ def current_id; '37'; end
[:delete, :delete!].each do |method|

describe method => :destroy, :id => '37' do
expects :find, :on => Task, :with => '37', :returns => mock_task
expects :destroy, :on => mock_task
expects :title, :on => mock_task, :with => false do |boolean|
expects :find, :on => Task, :with => '37', :returns => task_proc
expects :destroy, :on => task_proc
expects :title, :on => task_proc, :with => false do |boolean|
if boolean
'This should not appear'
else
Expand All @@ -239,7 +247,7 @@ def current_id; '37'; end
subject { controller }

should_assign_to :task
should_assign_to :task, :with => mock_task
should_assign_to :task, :with => task_proc
should_assign_to :task, :with_kind_of => Task

should_set_the_flash
Expand Down

0 comments on commit c601038

Please sign in to comment.