How to assert against an object passed to a mock repository

Viewed 135

I have a code construct as below in the SUT

public class SUT
{

    //...

    public void Process()
    {
            // Does some work and creates new myDto(), and assigns field values.
            Update(myDto);
    }

    private void Update(MyDto myDto)
    {
        _repository.Update(myDto);
    }
}

I am passing a mock for _repository in my unit tests. I would like to verify if the Fields are properly set before invoking the Update(MyDto myDto) method

Is there a way to get the reference of the argument object through mock?

I would like to assert against the fields of the passed object myDto.

Something like.

Assert.AreEqual(1, myDto.Field1);

If not what are my options.

I am using MSTest and Moq.

1 Answers

Mock<T> has the Callback method that you can use like this;

myMock
   .Setup(x => x.Update(It.IsAny<MyDtoType>()))
   .Callback<MyDtoType>(VerifyDto);

And the method for verifications;

public void VerifyDto(MyDtoType dto) 
{
   Assert.AreEqual(1, myDto.Field1);
}
Related