Creating a context in custom shoulda macro does not work.

Posted by Honza on Stack Overflow See other posts from Stack Overflow or by Honza
Published on 2010-04-25T14:56:27Z Indexed on 2010/04/25 15:03 UTC
Read the original article Hit count: 259

I have a custom should macro in my test_helper.rb which looks like this.

  def self.should_require_login(actions = [:index])
    if (actions.is_a? Symbol)
      actions = [actions]
    end
    context "without user" do
      actions.each do |action|
        should "redirect #{action.to_s} away" do
          get action
          assert_redirected_to login_path
        end
      end
    end
    if block_given?
      context "active user logged in" do
        setup do
          @user = Factory.create(:user)
          @user.register!
          @user.activate!
          login_as(@user)
        end
        yield
      end
    end
  end

I would like to use it like this:

should_require_login(:protected_action) do
  should "do something" do
    ...
  end
end

And I am expecting the "do something" test to run in the "active user logged in" context, but the test executes in the top context, like the "active user logged in" context never existed and I fail to see the reason why.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails