Search code examples
c#tddmoq

Setup Mock to return the same object, which I send to it?


I want to test some code:

public ViewModel FillClientCreateViewModel(ViewModel model){
    model.Phone = new Phone { Name = "Test"};

    model.Phone = _entityInitializer.FillViewModel(model.Phone);
}

I also want to setup FillViewModel to return the same object as I give to it.

My test:

     entityInitMock.Setup(x => x.FillViewModel(It.IsAny<PhoneViewModel>())).Returns(It.IsAny<PhoneViewModel>());

 var result = TestedInstance.FillClientCreateViewModel(CreateViewModel);

 result.Phone.Name.ShouldBe("Test");

But in this case my test fell - because result.Phone.Name was cleaned by my mock.

How can I setup mock to just give me the same object which i give to it.


Solution

  • entityInitMock.Setup(x => x.FillViewModel(It.IsAny<PhoneViewModel>()))
        .Returns((PhoneViewModel m) => m);
    

    The Moq QuickStart is a great reference for similar questions.