views:

1002

answers:

2
public myReturnObj MethodA(System.Linq.IGrouping<string, MyObject> group){
 ...
foreach (MyObject o in group)
{
    //business process
}
...
return myReturnObj; }

I want to set up NUnit Mock object for passing as a paramter and then check the result of MethodA in my unittest.

How do I mock this IGrouping?

+2  A: 

You might mock up a IGrouping(string, MyObject) the same way you'd mock up any interface?

DynamicMock myMockGrouping = new DynamicMock(typeof IGrouping<string, MyObject>);

Or, you could go with a more live version:

List<MyObject> inputs = GetInputs();
IGrouping<string, MyObject> myLiveGrouping = inputs
  .GroupBy(o => "somestring").First();
David B
Cool! thanks a lot, David. Your live version is what I want.
tongdee
A: 

I'm very new to mock object. First time in my head is try to instantiate a DynamicMock() object and then continue with it's ExpectAndReturn() method.

For IGrouping interface, there's only one property, Key. So if I want to set up ExpectAndReturn to make it work in foreach, maybe I have to go to implement the Current, Next(), Reset() of IEnumerator.

That's not easy to set up mock object and waste a lot of development time.

Now my solution is like this:

    //prepare expected list of objects that want to be tested
        List<MyObject> list = new List<MyObject>();
        list.Add(new MyObject() {BookingNo="111",...});
        list.Add(new MyObject() {BookingNo="111",...});

        // grouping objects in list
        IEnumberable<IGrouping<string, MyObject>> group = list.GroupBy(p => p.BookingNo);

//in my test method
myReturnObj  obj = MethodA(group.First());
Assert.xx(...);

Thank you very much, David B!

tongdee