Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

tdd

moq

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.

like image 500
Ivan Korytin Avatar asked Jan 14 '12 00:01

Ivan Korytin


1 Answers

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

The Moq QuickStart is a great reference for similar questions.

like image 184
TrueWill Avatar answered Nov 03 '22 00:11

TrueWill