public void should_use_using_statement_for_simplicity() { var tracer = new StringBuilder(); using (var demoDisposable = new DisposableWithTracingDemoClass(tracer)) { // blah, blah, ... } // change the variable value to fix the test. const string expectedTracingMessage = "constructor called.\r\ndispose called.\r\n"; Assert.Equal(expectedTracingMessage, tracer.ToString()); }
public void should_use_using_statement_for_simplicity() { var tracerContent = "nothing"; using (var demoDisposable = new DisposableWithTracingDemoClass()) { tracerContent = demoDisposable.TracerContent(); } // change the variable value to fix the test. const string expectedTracingMessage = ""; Assert.Equal(expectedTracingMessage, tracerContent); }
public void should_call_dispose_anyway_using_try_finally() { var tracer = new StringBuilder(); DisposableWithTracingDemoClass demoDisposable = null; try { demoDisposable = new DisposableWithTracingDemoClass(tracer); } finally { demoDisposable?.Dispose(); } // change variable value to fix test. const string expectedTracingMessage = "constructor called.\r\ndispose called.\r\n"; Assert.Equal(expectedTracingMessage, tracer.ToString()); }
public void should_call_dispose_anyway_using_try_finally() { DisposableWithTracingDemoClass demoDisposable = null; try { demoDisposable = new DisposableWithTracingDemoClass(); } finally { if (demoDisposable != null) { demoDisposable.Dispose(); } } // change variable value to fix test. const string expectedTracingMessage = ""; Assert.Equal(expectedTracingMessage, demoDisposable.TracerContent()); }
public void should_call_dispose_anyway_using_try_finally() { var tracer = new StringBuilder(); DisposableWithTracingDemoClass demoDisposable = null; try { demoDisposable = new DisposableWithTracingDemoClass(tracer); } finally { if (demoDisposable != null) { demoDisposable.Dispose(); } } // change variable value to fix test. string expectedTracingMessage = "constructor called." + Environment.NewLine + "dispose called." + Environment.NewLine; Assert.Equal(expectedTracingMessage, tracer.ToString()); }