문제

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

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top