使用moles为存根类创建实例调用实际的类构造函数

时间:2012-09-20 06:47:00

标签: pex-and-moles

我是微软Moles的新手。我试图为Moles生成的存根类创建实例,当我调试它时,调用我不需要的actaul类构造函数。如何在Moles中实现这一目标。提供了以下示例代码以便更好地理解

public class BaseClass
{
    public BaseClass()
    {
        MarkNew();
    }

    protected void MarkNew()
    {
        this.State = ObjectState.New;
        MarkChild();
    }

    protected virtual void MarkChild()
    {
    }

    public ObjectState State { get; private set; }
}

public class DerivedClass : BaseClass
{
    public DerivedClass() : base()
    {
    }

    public string FileName { get; set; }
    public string Value { get; set; }
}

[PexMethod]
[HostType("Moles")]
public void ReadContentTest(string content)
{
    // Arrange.
    SDerivedClass derivedObject = new SDerivedClass();
    derivedObject.FileName = "test.txt";
    var fs = new SIFileSystem();
    fs.ReadAllTextString = delegate(string fileName)
    {
        PexAssert.AreEqual(derivedObject.FileName, fileName);
        return content;
    };

    // Act.
    var test = new TestReaderWithStubs(fs);
    test.ReadContent(derivedObject);

    // Assert.
    PexAssert.IsTrue(content == derivedObject.Value);
}

提前致谢 问候 希瓦

0 个答案:

没有答案