--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
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