Newbie trying to use Moq for enumerable method

1,506 views
Skip to first unread message

AC

unread,
Sep 12, 2011, 5:04:17 PM9/12/11
to Moq Discussions
I'm trying to see if Moq is something I'd like to use in a new project
as the other mocking frameworks I've used are challenging IMHO. So for
instance, I have a method as such:

IEnumerable<PickList> GetPickLists();

I'm not sure how I'm supposed to mock this... I've tried something
like this, but I'm getting compliation errors (I know the following
Returns() isn't correct, but can't figure out what to put in the
Returns body:

var mockCrm = new Mock<ICrmProvider>();
mockCrm.Setup<IEnumerable<PickList>>(foo => foo.GetPickLists())
.Returns<IEnumerable<PickList>>({});

Also, trying to mock something like these two methods:
CustomerSyncResult ApplyActions(IEnumerable<CustomerAction> actions);
IEnumerable<Customer> GetCustomers(IEnumerable<string> crmIDs,
IEnumerable<string> emails);

I know I'm asking a blanket question, but I'm having a heck of a time
getting started. The CHM in the download doesn't have enough samples
for me and some of the tutorials out there seem to be using obsolete
methods as well as not covering enumerations which makes it tricky for
me :(

Any tips would be greatly appreciated.

Kenneth Xu

unread,
Sep 13, 2011, 6:34:33 PM9/13/11
to moq...@googlegroups.com
For empty enumerable:
.Returns(Enumerable.Empty<PickList>());
or
.Returns(new PickList[0]);

For a canned enumerable:

.Returns(new [] {pickList1, pickList2});

HTH,
Kenneth


Kaleb Pederson

unread,
Sep 13, 2011, 6:48:48 PM9/13/11
to moq...@googlegroups.com
I'm guessing you have a class that looks something like this:

interface ICrmProvider {
IEnumerable<PickList> GetPickLists();
}

When testing we want to test the interaction between two different
objects the system under test (i.e. the primary actor), and some
secondary actor. I can't tell exactly what primary actor it is you're
trying to test, so I'll make one up for illustrative purposes:

public class PickListDisplayer {
private ICrmProvider provider
public PickListDisplayer(ICrmProvider provider) { this.provider = provider; }
void DisplayPickList() { DisplayInternal(this.provider.GetPickList()); }
}

Assuming I want to test the interaction between the PickListDisplayer
and the ICrmProvider, I may want to verify that DisplayPickList calls
the provider's GetPickList() method. Using nunit syntax:

[Test] public void DisplayPickListUsesGetPickListMethod() {
var mockCrmProvider = new Mock<ICrmProvider>();
mockCrmProvider.Setup(c => c.GetPickList()).Returns(new PickList[0]);

var displayer = new PickListDisplayer();
displayer.GetPickList();

mockCrmProvider.Verify(c => c.GetPickList(), Times.Once());
}

If you aren't testing interactions but are instead using it more as a
stub as your post suggests, you'll want to return something other than
an empty array as Kenneth illustrated.

mockCrmProvider.Setup(c => c.GetPickList()).Returns(new []
{pickList1, pickList2});

--
Kaleb Pederson
Blog - http://kalebpederson.com
Twitter - http://twitter.com/kalebpederson

Reply all
Reply to author
Forward
0 new messages