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

我想设置 NUnit Mock 对象作为参数传递,然后在我的单元测试中检查 MethodA 的结果。

我如何模拟这个 IGrouping?

4

2 回答 2

2

您可以像模拟任何接口一样模拟 IGrouping(string, MyObject) 吗?

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

或者,您可以使用更实时的版本:

List<MyObject> inputs = GetInputs();
IGrouping<string, MyObject> myLiveGrouping = inputs
  .GroupBy(o => "somestring").First();
于 2009-01-14T05:19:53.637 回答
0

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!

于 2009-01-14T08:23:41.160 回答