views:

154

answers:

2

Asking this question again with smaller code sample:

  # this is a dummy shoulda macro that creates a context
  def self.macro_context
    context "macro" do
      yield
    end
  end

  # i am expecting this test to fail within the macro context
  context "some context" do
    macro_context do
      should "test" do
        fail
      end
    end
  end

So what I would expect is to see:

  1) Error:
  test: some context macro context should test. (TestClassName)

But I am getting only this:

So what I would expect is to see:

  1) Error:
  test: some context should test. (TestClassName)

Any idea what am I doing wrong?

+1  A: 

I have something similar in my code. And I did it like this into test/shoulda_macros/whatever_file.rb

def self.should_require_login(actions = [:index], &block)
 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

     merge_block(&block)
   end
 end
end
Francisco
the only difference seems to be in merge_block call, I have not seen that before, what does it do?
Honza
As it name says, it will merge the block that you pass with the block in which you're calling `merge_block`. In your case it will merge the given block inside the `context "active user logged in" do ... end`
Francisco
A: 

Thanks Francisco for the code, to fix this you cannot just yield the block inside of your new context, you have to use shoulda's merge_block method. It then should look like this:

  def self.macro_context(&block)
    context "macro" do
      merge_block(&block)
    end
  end
Honza