Sobes.tech
Junior — Middle

Could you explain what a Mock is and in what cases it is used in testing?

sobes.tech AI

Answer from AI

Mock is a stub object that simulates the behavior of a real object in tests. It is used to isolate the tested code from external dependencies to verify the logic without external influences.

Example: if a method depends on a database, instead of a real connection, a Mock is created that returns predefined data.

In C#, libraries like Moq are often used to create such objects:

var mockRepository = new Mock<IRepository>();
mockRepository.Setup(repo => repo.GetData()).Returns(new List<string> { "test1", "test2" });

var service = new Service(mockRepository.Object);
var result = service.ProcessData();

// Check the result without accessing the real database
Could you explain what a Mock is and in what cases it… - sobes.tech