Skip to content

Commit

Permalink
Allow mock class to be given using the :as option.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jun 6, 2009
1 parent f824ea5 commit 0a55a73
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions remarkable_rails/CHANGELOG
@@ -1,3 +1,5 @@
* Allow mock model class to be set using :as option.

* [DEPRECATION] By default all matchers perform expectations, use with_stubs => true
if you want otherwise.

Expand Down
Expand Up @@ -405,6 +405,12 @@ def describe(*args, &block)
#
# == Options
#
# * <tt>:as</tt> - 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"
#
# * <tt>:class_method</tt> - When set to false, does not create the
# class method which returns a proc.
#
Expand Down Expand Up @@ -440,6 +446,8 @@ def mock_models(*models)

models.each do |model|
model = model.to_s
klass = options[:as] || model.classify

if options[:class_method]
(class << self; self; end).class_eval <<-METHOD
def #{model}_proc; proc { mock_#{model} }; end
Expand All @@ -452,11 +460,12 @@ def #{model.pluralize}_proc; proc { [ mock_#{model} ] }; end

self.class_eval <<-METHOD
def mock_#{model}(stubs={})
@#{model} ||= mock_model(#{model.classify}, stubs)
@#{model} ||= mock_model(#{klass}, stubs)
end
METHOD
end
end
alias :mock_model :mock_models

end

Expand Down
15 changes: 11 additions & 4 deletions remarkable_rails/spec/action_controller/macro_stubs_spec.rb
Expand Up @@ -24,13 +24,13 @@ def current_id; '37'; end
self.class.respond_to?(:mock_projects).should be_true
end

it 'should create a class singular mock method' do
it 'should create a class singular proc 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
it 'should create a class plural proc method' do
self.class.respond_to?(:projects_proc).should be_false
self.class.mock_models :project
self.class.respond_to?(:projects_proc).should be_true
Expand All @@ -50,7 +50,14 @@ def current_id; '37'; end
self.respond_to?(:mock_project).should be_true
end

it 'should create procs which evals to a mock dynamically' do
it 'should allow the mock class to be set' do
self.class.mock_model :project, :as => "::Admin::Project"
lambda{
mock_project
}.should raise_error(NameError, "uninitialized constant Admin")
end

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

Expand All @@ -59,7 +66,7 @@ def current_id; '37'; end
@task.should_not be_nil
end

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

Expand Down

0 comments on commit 0a55a73

Please sign in to comment.