c# - How do I unit test a method like this using IOC -


i'm trying wtire unit tests function looks this:

public list<int> process(int input) {     list<int> outputlist = new list<int>();      list<int> list = this.dependency1.getsomelist(input);     foreach(int element in list)     {         // ... procssing element          //do more processing         int processedint = this.dependency2.dosomeprocessing(element);          // ... processing processedint          outputlist.add(processedint);     }     return outputlist; } 

i planning on mocking dependency1 , dependency2 in test cases i'm not sure how should set them up. in order setup dependency2.dosomeprocessing need know value of "element" each time called. figure out either need to:

  1. copy paste of logic process() method test case

  2. calculate values hand (in actual function involve hard coding doubles many decimal places test case)

  3. bite bullet , use actual implementation of dependency2 instead of mocking it.

none of these solutions seem me. i'm new ioc , mocking i'm hoping there's i'm not getting. if not of these solutions seems least bad?

thanks

edit: i'm using moq mock dependencies.

what testing?

the content of method calls 1 function list of values, calls function modify these values , returns modified values.

you should not testing logic gets values, or logic converts in test, seperate tests.

so mocking should (hand compllied moq)

var dependency1 = new mock<idependency1>()   .setup(d => d.getsomelist(it.isany<int>())   .returns(new list<int>(new [] { 1, 2, 3 });   var dependency2 = new mock<idependency2>()   .setup(d => d.dosomeprocessing(it.isany<int>())   .returns(x => x * 2); // can input value , use determine output value 

then run method , make sure returned list 2,4,6. validates processing performed on list.

you create different test makes sure getsomelist() works. , makes sure dosomeprocessing() works. dosomeprocessing() test require hand calculated values.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -