Rspec-rails 2 adding current account helper to all view specs

Hi,

in rspec-1 view specs i had the ability to say:

login_as(some_role_or_account)

which made some helper methods like

current_account

that where used in views and controllers return something valuable.

Implementation was based on the fact that the controller in rspec-1
was ApplicationController. My ApplicationController contained a method
for setting the current_account.

In Rspec2 this is broken as the controller in ViewExampleGroup is now
ActionView::TestCase::TestController.

Would anyone know of an elegant way to add functionality, like helper
methods, to this controller instance in all ViewExampleGroups only.

option 1, which i don’t like because it depends on the fact that
ViewExampleGroup uses ActionView::TestCase::TestController:

class ActionView::TestCase::TestController
attr_accessor :current_account
helper_method :current_account
end
module RSpec::Rails::ViewExampleGroup
def login_as account
controller.current_account = account
end
end

I would like to use some hook to add these methods only to the
controller instance used in the view specs, regardless of what class
it is.

There must be an elegant way, is there?

Cheers
Rob

Hi Rob,

On 27 Jan 2011, at 21:45, Rob Westgeest wrote:

that where used in views and controllers return something valuable.

  end

end

I would like to use some hook to add these methods only to the
controller instance used in the view specs, regardless of what class
it is.

There must be an elegant way, is there?

In your spec_helper, you can use the configuration block to tell RSpec
to extend (i.e. mix a module in) to each ExampleGroup:

RSpec.configure do |config|
config.extend(MySpecialLoginMethods)
end

So you can define your test helper methods like #login_as in a module
called MySpecialLoginMethods and then have it mixed in to each example
group.

You can also filter this, so it only does it to certain types of
ExampleGroup. I can’t remember exactly what the syntax is for filtering
on view specs, but something like this:

RSpec.configure do |config|
config.extend(MySpecialLoginMethods, :view)
end

Does that help?

cheers,
Matt

[email protected]
07974 430184