Link to home
Start Free TrialLog in
Avatar of Simon Leung
Simon Leung

asked on

Mock code in ASP.NET

Can anyone explain what the attached code mean ?
TestCode.png
Avatar of fcnatra
fcnatra
Flag of Spain image

There you are testing that: Having an "AudioBookApiController" that receives an "AudioService", when you call the "Get" method on the AudioBookApiController object (apiController), it returns an AudioBook instance.

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.
Avatar of Simon Leung
Simon Leung

ASKER

#1 audioServiceMock.Setup(x => x.GetById(It.IsAny<Guid>())).Returns(audioBook);

Does it initialize the audioServiceMock with audioBook ?

#2 var apiController = new AudioBookApiController(audioServiceMock.Object);
Does it return the audioBook object ?

Thx
ASKER CERTIFIED SOLUTION
Avatar of fcnatra
fcnatra
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial