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:
copy paste of logic process() method test case
calculate values hand (in actual function involve hard coding doubles many decimal places test case)
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
Post a Comment