1

私はこのような基本的な構造を持っています

class Automobile
  def some_method
    # this code sets up structure for child classes... I want to test this
  end
end

class Car < Automobile
  def some_method
    super
    # code specific to Car... it's tested elsewhere so I don't want to test this now
  end
end

class CompactCar < Car
  def some_method
    super
    # code specific to CompactCar... I want to test this
  end
end

からコードを実行せずCompactCarにテストするための推奨される方法は何ですか? 子クラスに必要な構造を提供するので、常にそれをテストしたいのですが、機能は他の場所でテストされており、努力を繰り返したくありません。AutomobileCarAutomobile#some_methodCar's

class_eval1 つの解決策はoverwriteを使用することCar#some_methodですが、これは理想的ではありません。これは、上書きされたメソッドがテスト中にそのまま残るためです (元のライブラリ ファイルを setup/teardown メソッドで再ロードしない限り...一種の醜い解決策です) )。また、単に への呼び出しをスタブ化してCar#some_methodも機能しないようです。

これを行うためのよりクリーンな/より一般的に受け入れられている方法はありますか?

4

1 に答える 1

1

特定のコードを別のメソッドに入れるだけです。スーパーから何も使用していないようです。あなたでない限り?

class CompactCar < Car
  def some_method
    super
    compact_car_specific_code
  end

  # Test this method in isolation.
  def compact_car_specific_code
    # code specific to CompactCar... I want to test this
  end
end
于 2013-02-12T14:09:37.387 に答える