c# - Unit test a void method with Mock? -


i want test void method mock.

 public class consoletargetbuilder : itargetbuilder {     private const string console_with_stack_trace = "consolewithstacktrace";     private const string console_without_stack_trace = "consolewithoutstacktrace";     private loggermodel _loggermodel;     private loggingconfiguration _nlogloggingconfiguration;      public consoletargetbuilder(loggermodel loggermodel, loggingconfiguration nlogloggingconfiguration)     {         _loggermodel = loggermodel;         _nlogloggingconfiguration = nlogloggingconfiguration;     }      public void addnlogconfigurationtypetagret()     {         var consoletargetwithstacktrace = new consoletarget();         consoletargetwithstacktrace.name = console_with_stack_trace;         consoletargetwithstacktrace.layout = _loggermodel.layout + "|${stacktrace}";         _nlogloggingconfiguration.addtarget(console_with_stack_trace, consoletargetwithstacktrace);          var consoletargetwithoutstacktrace = new consoletarget();         consoletargetwithoutstacktrace.name = console_without_stack_trace;         consoletargetwithoutstacktrace.layout = _loggermodel.layout;         _nlogloggingconfiguration.addtarget(console_without_stack_trace, consoletargetwithoutstacktrace);     } 

the thing not sure how test it. have primary code.

public class consoletargetbuilderunittests {     public static ienumerable<object[]> consoletargetbuildertestdata     {                 {             return new[]             {                  new object[]                 {                     new loggermodel(),                     new loggingconfiguration(),                     2                 }             };         }     }     [theory]     [memberdata("consoletargetbuildertestdata")]     public void consoletargetbuilder_should_add_a_console_target(loggermodel loggermodel, loggingconfiguration nlogloggingconfiguration, int expectedconsoletargetcount)     {         // arrange         var targetbuildermock = new mock<itargetbuilder>();         targetbuildermock.setup(x => x.addnlogconfigurationtypetagret()).verifiable();         // act         var consoletargetbuilder = new consoletargetbuilder(loggermodel, nlogloggingconfiguration);         // assert         assert.equal(expectedconsoletargetcount, nlogloggingconfiguration.alltargets.count);     } 

please guide me right direction.

it looks me don't need mock @ all. testing addnlogconfigurationtypetagret. not constructor.

[theory] [memberdata("consoletargetbuildertestdata")] public void consoletargetbuilder_should_add_a_console_target(loggermodel loggermodel, loggingconfiguration nlogloggingconfiguration, int expectedconsoletargetcount) {     // arrange     var consoletargetbuilder = new consoletargetbuilder(loggermodel, nlogloggingconfiguration);     // act     consoletargetbuilder.addnlogconfigurationtypetagret();     // assert     assert.equal(expectedconsoletargetcount, nlogloggingconfiguration.alltargets.count); } 

if don't want call addnlogconfigurationtypetagret yourself, should called in constructor. changes test :

[theory] [memberdata("consoletargetbuildertestdata")] public void consoletargetbuilder_should_add_a_console_target(loggermodel loggermodel, loggingconfiguration nlogloggingconfiguration, int expectedconsoletargetcount) {     // arrange     // act     var consoletargetbuilder = new consoletargetbuilder(loggermodel, nlogloggingconfiguration);     // assert     assert.equal(expectedconsoletargetcount, nlogloggingconfiguration.alltargets.count); } 

and class constructor should :

public consoletargetbuilder(loggermodel loggermodel, loggingconfiguration nlogloggingconfiguration) {     _loggermodel = loggermodel;     _nlogloggingconfiguration = nlogloggingconfiguration;     addnlogconfigurationtypetagret(); } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -