Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6239835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:27:36+00:00 2026-05-24T11:27:36+00:00

In a .NET windows app, I have a class named EmployeeManager. On instantiation, this

  • 0

In a .NET windows app, I have a class named EmployeeManager. On instantiation, this class loads employees into a List from the database that haven’t completed registration. I’d like to use EmployeeManager in unit test. However, I don’t want to involve the database.

From what I understand about this scenario, I need an IEmployeeManager interface, which is only used for testing purposes. This doesn’t seem right since the interface has no other use. However, it will allow me to create some EmployeeManager test class that loads employees without involving the database. This way, I can assign values that would have otherwise come from the database.

Is the above correct and do I need to Mock it? Mocking (Moq framework) seems to use lots of code just to do simple things such as assigning a property. I don’t get the point. Why mock when I can just create a simple test class from IEmployeeManager that will provide what I need?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:27:36+00:00Added an answer on May 24, 2026 at 11:27 am

    From what I understand about this scenario, I need an IEmployeeManager interface, which is only used for testing purposes. This doesn’t seem right since the interface has no other use.

    It’s well worth creating the interface. Note also that the interface actually has multiple purposes:

    1. The interface identifies roles or responsibilities provided by an actor. In this case, the interface identifies the roles and responsibilities of the EmployeeManager. By using an interface you’re preventing an accidental dependency on something database specific.
    2. The interface reduces coupling. Since your application won’t depend on the EmployeeManager, you’re free to swap out its implementation without needing to recompile the rest of the application. Of course, this depends on project structure, number of assemblies, etc., but it nevertheless allows this type of reuse.
    3. The interface promotes testability. When you use an interface it becomes much easier to generate dynamic proxies that allow your software to be more easily tested.
    4. The interface forces thought1. Ok, I kind of already alluded to it, but it’s worth saying again. Just using an interface alone should make you think about an object’s roles and responsibilities. An interface shouldn’t be a kitchen sink. An interface represents a cohesive set of roles and responsibilities. If an interface’s methods aren’t cohesive or aren’t almost always used together then it’s likely that an object has multiple roles. Though not necessarily bad, it implies that multiple distinct interfaces are better. The larger an interface the harder it is to make it covariant or contravariant and, therefore, more malleable in code.

    However, it will allow me to create some EmployeeManager test class that loads employees without involving the database…. I don’t get the point. Why mock when I can just create a simple test class from IEmployeeManager that will provide what I need?

    As one poster pointed out, it sounds like you’re talking about creating a stub test class. Mocking frameworks can be used to create stubs, but one of the most important features about them is that they allow you to test behavior instead of state. Now let’s look at some examples. Assume the following:

    interface IEmployeeManager {
        void AddEmployee(ProspectiveEmployee e);
        void RemoveEmployee(Employee e);
    }
    
    class HiringOfficer {
        private readonly IEmployeeManager manager
        public HiringOfficer(IEmployeeManager manager) {
            this.manager = manager;
        }
        public void HireProspect(ProspectiveEmployee e) {
            manager.AddEmployee(e);
        }
    }
    

    When we test the HiringOfficer‘s HireEmployee behavior, we’re interested in validating that he correctly communicated to the employee manager that this perspective employee be added as an employee. You’ll often see something like this:

    // you have an interface IEmployeeManager and a stub class
    // called TestableEmployeeManager that implements IEmployeeManager
    // that is pre-populated with test data
    [Test]
    public void HiringOfficerAddsProspectiveEmployeeToDatabase() {
        var manager = new TestableEmployeeManager(); // Arrange
        var officer = new HiringOfficer(manager); // BTW: poor example of real-world DI
        var prospect = CreateProspect();
        Assert.AreEqual(4, manager.EmployeeCount());
    
        officer.HireProspect(prospect); // Act
    
        Assert.AreEqual(5, manager.EmployeeCount()); // Assert
        Assert.AreEqual("John", manager.Employees[4].FirstName);
        Assert.AreEqual("Doe", manager.Employees[4].LastName);
        //...
    }
    

    The above test is reasonable… but not good. It’s a state-based test. That is, it verifies the behavior by checking the state before and after some action. Sometimes this is the only way to test things; sometimes it’s the best way to test something.

    But, testing behavior is often better, and this is where mocking frameworks shine:

    // using Moq for mocking
    [Test]
    public void HiringOfficerCommunicatesAdditionOfNewEmployee() {
        var mockEmployeeManager = new Mock<EmployeeManager>(); // Arrange
        var officer = new HiringOfficer(mockEmployeeManager.Object);
        var prospect = CreateProspect();
    
        officer.HireProspect(prospect); // Act
    
        mockEmployeeManager.Verify(m => m.AddEmployee(prospect), Times.Once); // Assert
    }
    

    In the above we tested the only thing that really mattered — that the hiring officer communicated to the employee manager that a new employee needed to be added (once, and only once… though I actually wouldn’t bother checking the count in this case). Not only that, I validated that the employee that I asked the hiring officer to hire was added by the employee manager. I’ve tested the critical behavior. I didn’t need even a simple test stub. My test was shorter. The actual behavior was much more evident — it becomes possible to see the interaction and validate interaction between objects.

    It is possible to make your stub test class record interactions, but then you’re emulating the mocking frameworks. If you’re going to test behavior — use a mocking framework.

    As another poster mentioned, dependency injection (DI) and inversion of control (IoC) are important. My example above isn’t a good example of this, but both should be carefully considered and judiciously used. There’s a lot of writing on the subject available.

    1 – Yes, thinking is still optional, but I’d strongly recommend it ;).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
JI have written a .NET C# Windows Form app in Visual Studio 2008 that
I have an old .net 2.0 windows app I need to deploy on a
We have about 7 app servers running .NET windows services that ping a single
I have a .net class library 2.0 with an app.config with a connectionString section.
I have written a .NET Windows service which has a WCF service built into
I am developing a .NET 3.5 Windows Forms app. I have two projects, the
I have a .Net 4 web app that uses Windows authentication and all works
I have a .NET 2.0 windows forms app, which makes heavy use of the
I have this ASP.NET app, in which I use Entity Framework to connect to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.