Mocking in ActionController

2 views
Skip to first unread message

Rafael Uchôa

unread,
Aug 18, 2010, 9:58:36 AM8/18/10
to Remarkable
Hi there,
I'm using Remarkable for the first time and loving! Everything is
simple as should be.
But I have a question.
I want to mocking dependencies my self, how can I do that?

Something like this:

describe Project::CatalogsController do
before(:all) { @project = Factory.create(:project) } #doesn't work!
params :project_id => @project.id

#describe :get => ....
end

And with it I can use:
should_redirect_to projects_path(@project)

If I use 'mock_project' I need to mocking the return of Project.find
all time, I don't want it.

Thanks in advance
Regards

Sébastien Nicouleaud

unread,
Aug 19, 2010, 5:35:27 AM8/19/10
to remarka...@googlegroups.com
You probably don't need factories in controller specs.

Something like this should work:


require 'spec_helper'

describe Project::CatalogsController do
mock_model :project

params :project_id => 'project id'
expects :find, :on => Project, :with => 'project id', :returns => project_proc

describe :get => :some_action do
# ...
should_redirect_to { projects_path(mock_project) }
end
end


Seb

Ho-Sheng Hsiao

unread,
Aug 19, 2010, 12:21:31 PM8/19/10
to Remarkable

You are using Remarkable 3?

If you are using Rspec 1.3 (the latest Rspec 1.x version), you can do
something like

describe Project::CatalogsController do
  let(:project) { Factory.create(:project) }
  params :project_id => project.id

  #describe :get => ....
end

Though that isn't going to work.

That may still not work. Just so you know, this interface is very
unlikely to be in Remarkable 4 (using Rspec 2 and Rails 3). I do
actually have a replacement for the controller tests though I'd have
to get my employer to release them into the open source since they
don't actually use the Remarkable macro system and uses pure Rspec 2
(meaning, no i18n).

Ho-Sheng Hsiao

Rafael Uchôa

unread,
Aug 19, 2010, 5:02:49 PM8/19/10
to Remarkable
Thanks all.

In fact, I resolve this with more 'verbose' code, with a lot of
'expectation' on my models per action.
When I refactor I try the suggestion of Sebastien, to put the
expectation on class level.

best regards.
Reply all
Reply to author
Forward
0 new messages