Mock objects are used when you are setting up automated testing. In theory these objects are like your normal API objects however they do not normally write to your database.
So if you had a bulletin class in the interface, you might also have a bulletinMock object.
Your bulletin class would contain methods like GetAllBulletins, Get1, Append, Update, and Delete. These methods would perform actual database updates.
Your bulletinMock class would contain the same methods, but they may just update data in memory.
When you execute your automated testing, you will want it to execute against the Mock objects.
A complexity is how do you manipulate your API so that your program calls business objects, but your test scripts use the mock objects?
If you consider an onion framework. A note class. It might have 4 layers. 1 - domain 2 - application 3 - infrastructures 4 - API
Generally speaking layer 4 might contain pointers to layer 3, 2, 1. Layer 3 might contain pointers to layer 2, 1, layer 2 might contain references to 1.
The API’s might call services located in the application layer.
If you were testing your API layer, you might create a mock object for the services layer.
The Note Service layer has the following interface:
[TestInitialize] public voidSetup() { // Arrange var noteDtoList = new List<NoteDto> { new NoteDto { ApplicationKey = "MyApp", ApplicationSubKey = "Notes", CompanyKey = "Test", Note = "This is a new note - 1", Who = "John Doe" }, new NoteDto { ApplicationKey = "MyApp", ApplicationSubKey = "Notes", CompanyKey = "Test", Note = "This is a new note 2", Who = "John Doe" }, // Add more NoteDto instances as needed };
var noteDto = new NoteDto { ApplicationKey = "MyApp", ApplicationSubKey = "Notes", CompanyKey = "Test", Note = "This is a new note - 1", Who = "John Doe" };
var result = new Result<List<NoteDto>>(noteDtoList); result.WasSuccessful = true;
_controller = new NoteController(_mockNoteService.Object);
// fluent validation validator = new NoteValidator(); }
If you look at the Setup() method, you see a very version one of the NoteService mock object.
You’ll notice a couple of things
We create a noteDtoList array - this will be a sample results of the GetAllData method.
You see a noteDto object - which is sample results of a GetData method.
I create a Mock object of type INoteService, then I setup methods using the .Setup().ReturnsAsync(r) pairs
The interface of the first method GetAllNotesAsync has several parameters, so our setup method creates placeholders for each of those parameters.
We declare a _controller variable. This is a pointer to the note controller. Then we inject the MockNoteService object into that. Then we can use the _controller variable in the rest of the class method.