c# - How would you test it? How many tests? -


i have class (it's more pseudocode)

 public class roles {     private processdata processdata;      roles(processdata pd)     {         processdata = pd;     }      public string[] getloginsthatcancallaction(string actionname)     {         return getpeoplethatcancallactionfromdb(actionname)             .union(processdata.processowner)             .union(getfromdb(role.administrators);             // there may many more union(xyz) calls here             .toarray();     }      // can refactor methods mockable     private getpeoplethatcancallactionfromdb(...)     private getfromdb(...) } 

now question is. write 1 test each union call in getloginsthaatcanrunaction method? or is enough write 1 test , assert method returns logins returned methods called inside getloginsthatcancallaction.

i can see reasons both ways. maybe convince me on or other solution.

edit: think wasn't clear question: wanted ask if write test

 var pd = new processdata() pd.processowner = "owner"; var r = new roles(processdata) setupsothatcallsforpeoplethatcancallactionwillreturn("joe"); setupsothatcallforadministratorswillreturn("administrator");  var logins = r.getloginsthatcancallaction("some action");  assert.that(logins, contains("owner"); assert.that(logins, contains("joe"); assert.that(logins, contains("administrator");  

or split 3 separate tests 1 assert in each one?

you should exercise public api of unit under test. unit has single public method getloginsthatcancallaction. not matter whether method call other methods, or implemented 1 large method. that's implementation details. matters whether method correctly communicates dependency , returns expected result:

// arrange mock<iprocessdata> processdata = new mock<iprocessdata>(); processdata.setup(d => d.processowner).returns(new[] { "bob" }); var expected = new []{ "bob", "joe" }; // act var actual = roles.getloginsthatcancallaction("drink"); // assert processdata.verifyget(d => d.processowner); // verify communication collectionassert.areequivalent(expected, actual); // verify result 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -