What is mocking?
π‘ Concept: Mocking in Unit Testing
Mocking is a technique to create fake implementations of dependencies in unit tests to isolate the system under test.
π Quick Intro
Mocks simulate behavior of complex, real objects, allowing tests to focus on logic without side effects.
π§ Analogy
Imagine rehearsing a play with stand-ins for some actors to test the main characterβs performance without distractions.
π§ Technical Explanation
- π οΈ Mocks replace real dependencies with controlled substitutes.
- π Allow verification of interactions and calls.
- π¦ Common in test-driven development (TDD).
- π§© Supported by frameworks like Moq, NSubstitute, and FakeItEasy.
- π Help isolate test cases to focus on business logic.
π― Use Cases
- β Isolating database or API calls.
- β Simulating error scenarios.
- β Testing components independently.
- β Ensuring expected interactions occur.
π» Code Example
using Moq;
using Xunit;
public interface IService {
int GetValue();
}
public class Consumer {
private readonly IService _service;
public Consumer(IService service) {
_service = service;
}
public int UseService() {
return _service.GetValue() + 1;
}
}
public class ConsumerTests {
[Fact]
public void TestUseService() {
var mock = new Mock();
mock.Setup(s => s.GetValue()).Returns(5);
var consumer = new Consumer(mock.Object);
var result = consumer.UseService();
Assert.Equal(6, result);
}
}

β Interview Q&A
Q1: What is mocking?
A: Creating fake implementations for testing.
Q2: Why use mocking?
A: To isolate the unit being tested.
Q3: Name some mocking frameworks.
A: Moq, NSubstitute, FakeItEasy.
Q4: Can mocks verify method calls?
A: Yes.
Q5: Is mocking part of TDD?
A: Yes.
Q6: Can mocks simulate errors?
A: Yes.
Q7: What is a stub?
A: A basic mock with fixed responses.
Q8: Difference between mock and stub?
A: Mock verifies behavior; stub provides canned data.
Q9: What is dependency injectionβs relation to mocking?
A: Allows easy replacement of dependencies.
Q10: Can mocks mock non-virtual methods?
A: Typically no; requires special frameworks.
π MCQs
Q1. What is mocking?
- Creating fake implementations
- Writing real code
- Ignoring tests
- Debugging
Q2. Why use mocking?
- Speed up code
- Isolate units under test
- Make code complex
- None
Q3. Name mocking frameworks.
- Moq, NSubstitute, FakeItEasy
- JUnit
- XUnit
- NUnit
Q4. Can mocks verify calls?
- No
- Yes
- Sometimes
- Never
Q5. Is mocking part of TDD?
- No
- Yes
- Sometimes
- Never
Q6. Can mocks simulate errors?
- No
- Yes
- Sometimes
- Never
Q7. What is a stub?
- Basic mock
- Basic mock with fixed response
- Real object
- Proxy
Q8. Mock vs stub?
- Mock and stub same
- Mock verifies, stub provides data
- Stub verifies, mock provides data
- None
Q9. Relation of DI to mocking?
- Makes code slow
- Enables dependency replacement
- Complicates testing
- None
Q10. Can mocks mock non-virtual methods?
- Yes
- No
- Sometimes
- Always
π‘ Bonus Insight
Mocking helps maintain isolated, reliable unit tests that lead to better software quality.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!