在使用Unit做单元测试遇到错误NUnit1032,提示信息如下:

NUnit1032: The field bag should be Disposed in a methodannotated with [OneTimeTearDownAttribute]
NUnit1032: The field bag should be Disposed in a methodannotated with [TearDownAttribute]

错误的原因详细信息如下:
https://docs.nunit.org/articles/nunit-analyzers/NUnit1032.html
翻译过来大概是:
IDisposable 字段/属性应在 TearDown 方法中释放。
此分析器规则仅适用于使用默认 NUnit SingleInstance 生命周期的 TestFixture,其中该类对所有测试实例化一次。
如果使用的是 LifeCycle.InstancePerTestCase,则应在测试类的 Dispose 方法中释放字段/属性。
总结一句话,在单元测试中IDispose对象需要在测试该当执行完了释放。

解决办法也很简单添加一个方法:

[TearDown]
[OneTimeTearDown]
public void Dispose()
{
    if (_bag != null)
        _bag.Dispose();
}

需要注意的是这个方法分别添加了   [TearDown]和 [OneTimeTearDown],会执行两次,所以需要处理一下重复执行问题。