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);

}

posted on 2013-05-16 15:46  Chopper  阅读(255)  评论(0编辑  收藏  举报