Rails: action helper for controller specs

Hey guys,

just wrote a helper for controller specs in the rails. Link to the gist

action helper for rspec · GitHub - to view with syntax highlight.

Helper +action+ allows you to write something like

describe “GET index” do

action { get :index }

context ‘if user signed in’ do

before { sign_in user }

it { should respond_with :success }

end

context ‘if user logged out’ do

it { should redirect_to sign_in_path }

end

end

instead of

describe “GET index” do

context ‘if user signed in’ do

before { sign_in user }

before { get :index }

it { should respond_with :success }

end

context ‘if user logged out’ do

before { get :index }

it { should redirect_to sign_in_path }

end

end

def action(&block)
before { self.class.before(&block) unless action_added?(&block) }

end

def action_added?(&block)
self.class.hooks[:before][:each].map(&:to_proc).include? block

end

What do you think?

  • Alex

On Wed, Jul 13, 2011 at 11:06 AM, Alexander Glushkov
[email protected]wrote:

context ‘if user signed in’ do

end

def action_added?(&block)


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

So this is just an alias to before?

Not exactly. It adds a before { ‘your action call’ } to the end of hooks
collection,
so that it will called at the very end, right before the spec call.
Compare two examples I provided in the comments.