I'm just starting with unit testing and there is a scenario, I'm not sure how uplined and not my solution Feel the right.
I have a little code that does something, if it fails, an exception exception is stripped and logged as below.
publicT CreateTypedObjectInstance & lt; T & gt; () {T o = default (t); Try {O = Activator. Credit Instance & lt; T & gt; (); } Hold (Exception Pre) {LogError (ex); East; } Return O; } Private Zero LogError (Exception) {if (logger! = Null) {logger.LogError (ex); }} I want to test that if any error has been thrown, then this log calls the eraser () method which in turn makes another object call.
I contacted him to catch the fake and threw the first exception and then emphasized the LogError method. However, it does not feel the right need to catch the exception? I read something reminiscent that it is bad to try to catch in tests? Is there any other way to do this test or should I re-apply the argument? Any ideas would be great!
[Test] Public Zero CreateTypedObjectInstance_GivenTypeWithoutPrivateContructor_LogErrorToLogger () {// Setup Method MockRepository Subhash = New MockRepository (); Illogger mocklog ger = Mockery. Stick mock & lt; Illogger & gt; (); Generic ObjectFirst Instance.logger = Mocklogger; Hope. Call (Rep. {Mocklogger. Log error (null);}). Next Aggregations (); Mockery.ReplayAll (); // This will throw an error because the parameter does not try the parameter constructor {generic object factory instance. Create Type Object instance & lt; String & gt; (); } Ignore this error to test the behavior after annoying. Please verify (); Using Mark Rascoof, it is answered that the test becomes and a magic Works like. [TEST] Public Zero CreateTypedObjectInstance_GivenTypeWithoutPrivateContructor_LogErrorToLogger () {// Setup Method MockRepository Fake = New MockRepository (); Illogger mocklog ger = Mockery. Stick mock & lt; Illogger & gt; (); Generic ObjectFirst Instance.logger = Mocklogger; Hope. Call (Rep. {Mocklogger. Log error (null);}). Next Aggregations (); Mockery.ReplayAll (); Deny.Thank you & lt; Missing method reception & gt; (() = & Gt; Generic Object Factory Instance. Constructed Object Instance & lt; String & gt; ()); Mockery.VerifyAll (); }
You did not say which NUITT you are using, but The preferred approach will be the method only.
Assert.Throws & lt; YourCustomException & gt; (() = & Gt; Generic object Factory Instance. CreateTad Object instance & lt; string & gt; ()); It is appropriate when your test framework does not provide a feature to check the exceptions thrown.
Comments
Post a Comment