3

我有一个测试类,里面有一些 IDisposable 项目。这是我第一次尝试做这样的事情:

private MemoryStream toolExampleMs;
private MemoryStream issueClassExampleMs;
private MemoryStream issueTypeExampleMs;
private MemoryStream uniqueIdExampleMs;

private Check exampleCheck;

public SuppressionDatabaseTest()
{
    this.toolExampleMs = new MemoryStream(Encoding.UTF8.GetBytes(toolExample));
    this.issueClassExampleMs = new MemoryStream(Encoding.UTF8.GetBytes(toolExample));
    this.issueTypeExampleMs = new MemoryStream(Encoding.UTF8.GetBytes(issueTypeExample));
    this.uniqueIdExampleMs = new MemoryStream(Encoding.UTF8.GetBytes(uniqueIdExample));

    this.exampleCheck = new Check();
    this.exampleCheck.IssueClass = "FooBarClass";
    this.exampleCheck.IssueType = "FooBarType";
    this.exampleCheck.Key = "FooBarExactWith?Unicode";
}

[ClassCleanup]
public void CleanupAll() // Error: CleanupAll has the wrong signature
{
    toolExampleMs.Dispose();
    issueClassExampleMs.Dispose();
    issueTypeExampleMs.Dispose();
    uniqueIdExampleMs.Dispose();
}

[TestCleanup]
public void Cleanup()
{
    this.toolExampleMs.Seek(0, SeekOrigin.Begin);
    this.issueClassExampleMs.Seek(0, SeekOrigin.Begin);
    this.issueTypeExampleMs.Seek(0, SeekOrigin.Begin);
    this.uniqueIdExampleMs.Seek(0, SeekOrigin.Begin);
}

不幸的是,ClassCleanup 方法必须static在 MSTest 中,这意味着没有地方可以挂钩要处置的调用。这是否意味着我需要在每次单独测试之前和之后重建这些流?

4

1 回答 1

3

简短的回答是肯定的。您需要在每次测试之前重建这些流,并在每次测试后处理它们。这很容易使用 [TestCleanup] 和 [TestInitialize] 属性。

因此,不要在测试类中构建流: SuppressionDatabaseTest 使用 [TestInitialize]

于 2012-09-04T22:18:35.733 回答