简体   繁体   English

NUnit是否处置实现IDisposable的对象?

[英]Does NUnit dispose of objects that implement IDisposable?

Does NUnit dispose of objects that implement IDisposable on cleanup? NUnit是否处置清除时实现IDisposable的对象? I realize there are various ways I can get an object disposed of in a method, but, for example, if the method fails prior to the object being disposed - will NUnit take care of it? 我意识到可以通过多种方法处理对象,但是例如,如果该方法在对象被处理之前失败了-NUnit会处理吗? (For reference, i'm on v2.6+) (供参考,我使用的是v2.6 +)

The specific reason i'm asking is for a case where an object that implements IDisposable is created, but I am asserting that an exception is thrown upon creation. 我要问的特定原因是在创建实现IDisposable的对象的情况下,但是我断言在创建时会引发异常。 If the test fails - and the object is created, I do not want to run into memory leak problems. 如果测试失败-并且创建了对象,则我不想遇到内存泄漏问题。

Example: 例:

//Will the StreamReader instance here be disposed 
//Of if the assertion fails, and the instance is created?
Assert.Throws<Exception>(() => new StreamReader(filename));

I realize that this will work: 我意识到这将起作用:

Assert.Throws<Exception>(() => 
{
    using (StreamReader sr = new StreamReader(filename)) { }
}

But it just seems like unnecessary code if NUnit will take care of disposing when necessary. 但是,如果NUnit会在必要时进行处理,这似乎是不必要的代码。

No, NUnit will not dispose your objects when used that way. 否,以这种方式使用时,NUnit不会处置您的对象。 NUnit 3.x will dispose test fixtures that are IDisposable, but that is it. NUnit 3.x会放置IDisposposable的测试装置,仅此而已。

You state that it seems unnecessary to dispose because NUnit can do it for you, but that is incorrect. 您声明似乎没有必要进行处置,因为NUnit可以为您做到这一点,但这是不正确的。 In the code in your example, it looks to you like you are passing NUnit an IDisposable object, but in fact you are passing a delegate/lambda/code block that happens to contain an IDisposable object. 在示例代码中,看起来好像您正在向NUnit传递IDisposable对象,但实际上,您正在传递恰好包含IDisposable对象的委托/ lambda /代码块。

You will notice that the signature for Assert.Throws is; 您会注意到Assert.Throws的签名是;

public static TActual Throws<TActual>(TestDelegate code) where TActual : Exception

Notice that it takes a TestDelegate , not an object. 请注意,它接受一个TestDelegate ,而不是一个对象。 TestDelegate is just a void delegate, TestDelegate只是一个无效的委托,

public delegate void TestDelegate();

You are reading your code as if you are passing in a StreamReader , but you are actually passing in a delegate, or in other words a method that NUnit calls. 您正在读取代码,就像在传递StreamReader ,但是实际上是在传递委托,或者换句话说,传递了NUnit调用的方法。 NUnit doesn't know or care what you do in that method. NUnit不知道或不在乎您在该方法中所做的事情。 Like any other method, it is up to you to dispose objects you create. 像任何其他方法一样,由您决定要创建的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 处理所有实现IDisposable的嵌套对象 - Dispose of nested objects which all implement IDisposable 如果没有Dispose方法,这个类如何实现IDisposable? - How does this class implement IDisposable if it doesn't have a Dispose method? C#编译器是否自动处理IDisposable对象? - Does the C# compiler automatically dispose of IDisposable objects? 在 IDisposable 对象数组中处置()还是不处置()元素? - To Dispose() or Not To Dispose() elements in an array of IDisposable objects? 如何处置实现IDiposable的对象以及在其上调用的属性/方法的类型实现IDisposable - how to dispose off objects that implement IDiposable and also the types of properties/methods invoked on them implement IDisposable 如何处置具有无法实现IDisposable的属性的类? - How to dispose of a class with properties that do not implement IDisposable? 实施Dispose但不实施IDisposable背后的原因? - Reasoning behind implementing Dispose but not implement IDisposable? 处置未实现IDisposable的第三方类使用的资源 - Dispose of resources used by 3rd party class which does not implement IDisposable 创建IDisposable对象的工厂,如何处置它们? - Factory that creates IDisposable objects, how to dispose them? 我们可以对未实现IDisposable的对象使用Using语句吗 - Can we use Using statement for objects which does not implement IDisposable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM