Question

Given the following snippet from my test:

var mockProvider = MockRepository.GenerateMock<IItemProvider>();

var target = new ItemService(mockProvider);

target.SaveItem(item);

Internally target.SaveItem makes a call like this:

provider.SaveItem(new SaveContract(item.Id, user, contents)); where provider is the local name for the mockProvider passed in.

How do I:

  1. Verify provider.SaveItem is called whilst also
  2. Asserting that the values of item.Id, user and contents are as they should be.

I think I might be able to use mockProvider.AssertWasCalled but can't figure out the syntax to set the condition of the parameters passed to the constructor of SaveContract.

TIA

Was it helpful?

Solution

Ok so based on this I did something like the following:

var mockProvider = MockRepository.GenerateMock<IItemProvider>();
var target = new ItemService(mockProvider);
Item testItem = null;

mockProvider.Expect(c => c.SaveItem(Arg<Item>.Is.Anything))
.WhenCalled(call =>
{
    testItem = (Item)call.Arguments[0];
});


target.SaveItem(item);//item initialised elsewhere


Assert.AreEqual(item.Id, testItem.Id);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top