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

コードを提供してくれたフランシスコに感謝します。これを修正するには、新しいコンテキスト内でブロックを生成することはできません。shoulda のmerge_blockメソッドを使用する必要があります。次に、次のようになります。

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