mocking view helper methods in Rspec integration spec

290 views
Skip to first unread message

LesNightingill

unread,
Dec 4, 2012, 4:47:01 AM12/4/12
to rs...@googlegroups.com
I have Rails app with a helper method "current_user_permitted?" in app/helpers/application_helper.rb
Since it has some complex dependencies, I want to mock it for my Rspec integration tests.

The question is:
On what class do I need to create a mock, so that I can force this helper to always return true?

Thanks in advance

David Chelimsky

unread,
Dec 4, 2012, 8:06:53 AM12/4/12
to rs...@googlegroups.com
On Tue, Dec 4, 2012 at 3:47 AM, LesNightingill <codeh...@comcast.net> wrote:
> I have Rails app with a helper method "current_user_permitted?" in
> app/helpers/application_helper.rb
> Since it has some complex dependencies, I want to mock it for my Rspec
> integration tests.

Not recommended (integration tests are intended to test those
dependencies), but you can use any_instance on each of the controllers
that might be used in the example:

FooController.any_instance.stub(:current_user_permitted? => true)

HTH,
David

>
> The question is:
> On what class do I need to create a mock, so that I can force this helper to
> always return true?
>
> Thanks in advance
>
> --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To post to this group, send email to rs...@googlegroups.com.
> To unsubscribe from this group, send email to
> rspec+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rspec/-/dSOQ4tdqr_MJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Perry Smith

unread,
Dec 4, 2012, 11:16:41 AM12/4/12
to rs...@googlegroups.com

On Dec 4, 2012, at 7:06 AM, David Chelimsky wrote:

> On Tue, Dec 4, 2012 at 3:47 AM, LesNightingill <codeh...@comcast.net> wrote:
>> I have Rails app with a helper method "current_user_permitted?" in
>> app/helpers/application_helper.rb
>> Since it has some complex dependencies, I want to mock it for my Rspec
>> integration tests.
>
> Not recommended (integration tests are intended to test those
> dependencies), but you can use any_instance on each of the controllers
> that might be used in the example:
>
> FooController.any_instance.stub(:current_user_permitted? => true)

New to the group so this is as much a question as a suggestion:

can you attach it to ApplicationController ?

David Chelimsky

unread,
Dec 4, 2012, 11:25:48 AM12/4/12
to rs...@googlegroups.com
You can if it's a class method, not an instance method.
Reply all
Reply to author
Forward
0 new messages