1

用更小的代码示例再次问这个问题:

  # 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

所以我期望看到的是:

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

但我得到的只是这个:

所以我期望看到的是:

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

知道我在做什么错吗?

4

2 回答 2

2

我的代码中有类似的东西。我是这样做的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
于 2010-04-26T08:30:03.577 回答
1

感谢 Francisco 的代码,要解决这个问题,您不能只在新上下文中生成块,您必须使用 shoulda 的merge_block方法。然后它应该看起来像这样:

  def self.macro_context(&block)
    context "macro" do
      merge_block(&block)
    end
  end
于 2010-04-26T09:38:25.470 回答