Simon Leung
asked on
Mock code in ASP.NET
Can anyone explain what the attached code mean ?
TestCode.png
TestCode.png
ASKER
#1 audioServiceMock.Setup(x => x.GetById(It.IsAny<Guid>() )).Returns (audioBook );
Does it initialize the audioServiceMock with audioBook ?
#2 var apiController = new AudioBookApiController(aud ioServiceM ock.Object );
Does it return the audioBook object ?
Thx
Does it initialize the audioServiceMock with audioBook ?
#2 var apiController = new AudioBookApiController(aud
Does it return the audioBook object ?
Thx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To do so: On the test you seem to be testing just the AudioBookApiController, not the service, so you create a Mock of the service (audioServiceMock) and then you configure the apiController instance with that mock (during the construction of the AudioBookApiController).
You also tell what will audioServiceMock return when "GetById" is called, --> it must return the variable audioBook.
Finally, you tell the apiController to Get the audioBook related with a Guid and you check (Assert.Equal) that the audioBook returned is the expected.