Moles - Stub types

The Moles Framework. The need for a stub framework that could be effectively used with Pex initially motivated the creation of the Moles framework for generating stub types and mole types:

  •  Stub types make it easy to test code that consumes interfaces, or non-sealed classes with overridable methods.
  •  Mole types allow detouring of hard-coded dependencies on static or non-overridable methods.

Stub types sample.

public interface IStudent
{
  string GetName(string name);
}

public class ImpIStudent : StubBase, IStudent
{
  public Func<string, string> GetNameString;
  public string GetName(string name)
  {
    if (GetNameString != null)
    {
      return GetNameString("str");
    }
    return null;
  }
}


[TestMethod()]
public void GetNameTest()
{
  IStudent target = CreateIStudent();
  string expected = "name...";

  var istu = new ClassLibrary1.Moles.SIStudent()
  {
    GetNameString = (name) => { return expected; }
  };

  IStudent stu = istu;

  string actual = stu.GetName(expected);

  Assert.AreEqual(expected, actual);

}

你可能感兴趣的:(type)