I’m trying to wtire unit tests for a function that looks something like 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)
{
// ... Do some procssing to element
//Do some more processing
int processedInt = this.dependency2.DoSomeProcessing(element);
// ... Do some processing to processedInt
outputList.Add(processedInt);
}
return outputList;
}
I was planning on mocking dependency1 and dependency2 in the test cases but I’m not sure how I should set them up. In order to setup dependency2.DoSomeProcessing I will need to know what the value of “element” will be each time it is called. To figure this out I would either need to:
-
Copy paste some of the logic from my Process() method into the test case
-
Calculate the values by hand (in the actual function this would involve hard coding some doubles with many decimal places into the test case)
-
Bite the bullet and use the actual implementation of dependency2 instead of mocking it.
None of these solutions seem very good to me. I’m new to IOC and mocking so I’m hoping there’s just something I’m completely not getting. If not which of these solutions seems the least bad?
Thanks
Edit: I’m using Moq to mock the dependencies.
What exactly are you testing?
The content of this method is that it calls one function to get a list of values, then calls another function to modify these values and returns the modified values.
You should not be testing the logic that gets the values, or the logic that converts it in this test, they are seperate tests.
So your mocking should be something like (hand compllied with Moq)
Then just run your method and make sure the returned list is 2,4,6. This validates that some processing was performed on some list.
You then create a different test that makes sure
GetSomeList()works. And another that makes sureDoSomeProcessing()works. Its theDoSomeProcessing()test that will require hand calculated values.