Проверка того, что метод вызывает super в Ruby - PullRequest
0 голосов
/ 08 марта 2011

У меня есть библиотека тестирования, чтобы помочь в ведении журнала тестирования:

require 'stringio'

module RedirectIo
  def setup
    $stderr = @stderr = StringIO.new
    $stdin = @stdin = StringIO.new
    $stdout = @stdout = StringIO.new
    super
  end

  def teardown
    $stderr = STDERR
    $stdin = STDIN
    $stdout = STDOUT
    super
  end
end

Она предназначена для использования так:

require 'lib/redirect_io'

class FooTest < Test::Unit::TestCase
  include RedirectIo

  def test_logging
    msg = 'bar'
    Foo.new.log msg
    assert_match /^#{TIMESTAMP_REGEX} #{msg}$/, @stdout.string, 'log message'
  end
end

Конечно, у меня есть модульный тестмоя тестовая библиотека:)

require 'lib/redirect_io'

class RedirectIoTest < Test::Unit::TestCase
  def test_setup_and_teardown_are_mixed_in
    %W{setup teardown}.each do |method|
      assert_not_equal self.class, self.class.new(__method__).method(method).owner, "owner of method #{method}"
    end

    self.class.class_eval { include RedirectIo }
    %W{setup teardown}.each do |method|
      assert_equal RedirectIo, self.class.new(__method__).method(method).owner, "owner of method #{method}"
        end
  end

  def test_std_streams_captured
    %W{STDERR STDIN STDOUT}.each do |stream_name|
      assert_equal eval(stream_name), self.class.new(__method__).instance_eval("$#{stream_name.downcase}"), stream_name
    end

    self.class.class_eval { include RedirectIo }

    setup
    %W{STDERR STDIN STDOUT}.each do |stream_name|
      assert_not_equal eval(stream_name), self.class.new(__method__).instance_eval("$#{stream_name.downcase}"),
          stream_name
    end

    teardown
    %W{STDERR STDIN STDOUT}.each do |stream_name|
      assert_equal eval(stream_name), self.class.new(__method__).instance_eval("$#{stream_name.downcase}"), stream_name
    end
  end
end

Мне не удается понять, как проверить, что RedirectIo.setup и teardown вызывают super.Есть идеи?

1 Ответ

1 голос
/ 08 марта 2011

Я нашел один способ сделать это, что на самом деле делает тестовый код намного чище!

require 'lib/redirect_io'

class RedirectIoTest < Test::Unit::TestCase
  class RedirectIoTestSubjectSuper < Test::Unit::TestCase
    def setup
      @setup = true
    end
    def teardown
      @teardown = true
    end
  end

  class RedirectIoTestSubject < RedirectIoTestSubjectSuper
    include RedirectIo
    def test_fake; end
  end

  def test_setup_and_teardown_are_mixed_in
    %W{setup teardown}.each do |method|
      assert_not_equal RedirectIoTestSubject, self.method(method).owner, "owner of method #{method}"
    end

    %W{setup teardown}.each do |method|
      assert_equal RedirectIo, RedirectIoTestSubject.new(:test_fake).method(method).owner, "owner of method #{method}"
    end
  end

  def test_std_streams_captured
    obj = RedirectIoTestSubject.new(:test_fake)
    $stderr = STDERR
    $stdin = STDIN
    $stdout = STDOUT

    obj.setup
    %W{STDERR STDIN STDOUT}.each do |stream_name|
      assert_not_equal eval(stream_name), eval("$#{stream_name.downcase}"), stream_name
    end

    obj.teardown
    %W{STDERR STDIN STDOUT}.each do |stream_name|
      assert_equal eval(stream_name), eval("$#{stream_name.downcase}"), stream_name
    end
  end

  def test_setup_and_teardown_call_super
    obj = RedirectIoTestSubject.new(:test_fake)
    obj.setup
    assert obj.instance_eval{@setup}, 'super called?'
    obj.teardown
    assert obj.instance_eval{@teardown}, 'super called?'
  end
end
...