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!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: