1

我是 Microsoft 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);
}

提前致谢 问候西瓦

4

0 回答 0