How do you mock dependencies in unit tests using DI?
- Use mocking libraries like Moq, NSubstitute, or FakeItEasy.
- Create mocks of interfaces and inject them into the class under test:
var mockRepo = new Mock<IProductRepository>();
mockRepo.Setup(repo => repo.GetAll()).Returns(new List<Product> {
... });
var service = new ProductService(mockRepo.Object);
// Act & Assert
- This allows testing in isolation without hitting real databases or external services.