0

組み込みの抽象クラスのメソッドを使用してブール値を作成するメソッド(runMethod())を単体テストする必要があります。抽象クラスのメソッドは、XmlDocumentsとノードを使用して情報を取得します。コードは次のようになります(これは非常に単純化されていますが、私の問題を示しています)

namespace AbstractTestExample
{
public abstract class AbstractExample
{
    public string propertyValues;
    protected XmlNode propertyValuesXML;
    protected string getProperty(string propertyName)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(new System.IO.StringReader(propertyValues));
        propertyValuesXML= doc.FirstChild;

        XmlNode node = propertyValuesXML.SelectSingleNode(String.Format("property[name='{0}']/value", propertyName));
        return node.InnerText;
    }
}

public class AbstractInheret : AbstractExample
{
    public void runMethod()
    {
        bool addIfContains = (getProperty("AddIfContains") == null || getProperty("AddIfContains") == "True");
        //Do something with boolean
    }
}
}

したがって、コードは作成されたXmlDocumentからプロパティを取得し、それを使用して結果をブール値に形成します。ここで私の質問は、ブール値の結果の動作を確実に制御するための最良の解決策は何であるかということです。モックの可能性のためにMoqを使用しています。

このコード例はおそらく少しあいまいですが、私が示すことができる最高のものです。皆さんがお役に立てば幸いです。

編集:基本的に必要なのは
、AbstractInheretクラスのテスト中にgetProperty()を制御できる必要があることです。

4

4 に答える 4

1

ブール値はxmlによって制御されるため、コードをリファクタリングして、xmlを簡単に設定できます。

このような:

namespace AbstractTestExample
{
    public abstract class AbstractExample
    {
        protected XmlNode propertyValuesXML;

        protected string getProperty(string propertyName)
        {
            XmlNode node = propertyValuesXML.FirstChild.SelectSingleNode(String.Format("property[name='{0}']/value", propertyName));
            return node.InnerText;
        }
    }

    public class AbstractInheret : AbstractExample
    {
        public AbstractInheret(string propertyValues){

            propertyValuesXML = new XmlDocument();
            propertyValuesXML.Load(new System.IO.StringReader(propertyValues));
        }

        public void runMethod()
        {
            bool addIfContains = (getProperty("AddIfContains") == null || getProperty("AddIfContains") == "True");
            //Do something with boolean
        }
    }
}

このようにして、何もモックする必要がなく、xmlの文字列をクラスに簡単に渡すことができます。また、コンストラクターに渡された無効なxmlに関連するエラー状態を確認する必要があります。

于 2010-04-15T08:48:33.110 に答える
0

私があなたの質問を正しく理解しているなら、異なるブール値でaddIfContainsをテストできるように、getPropertyの結果を制御する方法を知りたいと思います。

何かのテストを書くのが難しい場合は、テストをやりすぎているか、コードの設計に問題があることを意味します。

継承の使用を回避することで、このコードをよりテストしやすくすることができる場合があります。ご存知のように、単体テストでは継承を処理するのが難しいことが多く(多くの緊密な結合が行われています)、委任は通常、とにかくうまく機能します。デザインに次の変更を加えることをお勧めします。

namespace TestExample
{
    public interface PropertyManager {
      public string getProperty(string propertyName);
    }

    public class XmlPropertyManager: PropertyManager {
      public string getProperty(string propertyName) {
      //...xml parsing code here
      }
    }

    public class Example {
      private PropertyManager propertyManager;

      public void runMethod() {
        bool addIfContains = (propertyManager.getProperty("AddIfContains") == null || propertyManager.getProperty("AddIfContains") == "True");
        //Do something with boolean
      }
    }
}

次に、PropertyManagerを簡単にモックアウトできます。

デザインで抽象基本クラス(getProperty以外の他の関数)を使用する必要がある場合でも、デリゲートアプローチを使用できます。

namespace TestExample
{
    public interface PropertyManager {
      public string getProperty(string propertyName);
    }

    public class XmlPropertyManager: PropertyManager {
      public string getProperty(string propertyName) {
      //...xml parsing code here
      }
    }

    public abstract class AbstractExample
    {
       protected PropertyManager propertyManager;

       //... other important methods
    }

    public class AbstractInheret: AbstractExample {

      public void runMethod() {
        bool addIfContains = (propertyManager.getProperty("AddIfContains") == null || propertyManager.getProperty("AddIfContains") == "True");
        //Do something with boolean
      }
    }
}
于 2010-04-14T12:22:36.530 に答える
0

この質問が正確に何を意味するのかよくわかりません-

「ブール値の結果の動作を確実に制御するための最良の解決策は何ですか。」

プロパティのブール値が必要だと思います。だから、これはちょうどいいかもしれません!

    public static bool GetBoolean(string boolStr)
    {
        bool bVal = default(bool);
        try
        {
            bVal = Convert.ToBoolean(boolStr);
        }
        catch (Exception) {
            bVal = default(bool);
        }
        return bVal;
    }

それをコードに差し込むだけで、それは良いはずです。:)

于 2010-04-14T13:08:23.543 に答える
0

何らかの理由で私の最初の回答で説明した方法でコードをリファクタリングできない場合(サードパーティが抽象基本クラスを提供しているなど)、getPropertyメソッドのみをオーバーライドするAbstractInheritのサブクラスを作成できます。テストをクラスの内部実装に結合するため、このタイプのアプローチは本当に好きではないことに注意してください。しかし、時には選択の余地がありません。

于 2010-04-15T12:04:14.823 に答える