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 6076957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:39:59+00:00 2026-05-23T10:39:59+00:00

Typically when I need to mock out a class for testing, I’ll use a

  • 0

Typically when I need to mock out a class for testing, I’ll use a library such as Rhino Mocks. Here I have a class called MyService that expects a IEmailSender.

public class MyService
{
    private readonly IEmailSender sender;

    public MyService(IEmailSender sender)
    {
        this.sender = sender;
    }

    public void Start()
    {
        this.sender.SendEmail();
    }
}

If I needed to test the interaction between these two objects, my test would look something like this:

[TestMethod]
public void Start_Test_Using_Rhino_Mocks()
{
    IEmailSender emailSender = MockRepository.GenerateMock<IEmailSender>();

    MyService service = new MyService(emailSender);
    service.Start();

    emailSender.AssertWasCalled
        (
            x => x.SendEmail(),
            c => c.Repeat.Once()
        );
}

In the test above, I’m using Rhino Mocks to generate the mock and assert that the SendEmail() method was called once.

But what if I could not use Rhino Mocks and had to create manual mocks?

public class MockEmailSender : IEmailSender
{
    public void SendEmail()
    {
    }
}

[TestMethod]
public void Start_Test_Using_Manual_Mocks()
{
    MockEmailSender emailSender = new MockEmailSender();

    MyService service = new MyService(emailSender);
    service.Start();

    // How do I test the interaction?
}

With the mock that I created manually, how would I verify that the SendEmail() method was called? I could put my assertions in the SendEmail() method of the mock, but that would make the test hard to understand since I don’t immediately see what’s going on when I look at the test.

  • 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-23T10:40:00+00:00Added an answer on May 23, 2026 at 10:40 am

    A very simple solution would have your manual mock just be a stateholder, with counters for the calls to each method. But it’s fragile …

    public class MockEmailSender : IEmailSender
    {
        public int SendCount = 0;
    
        public void SendMail(...)
        {
            SendCount++;
        }
    
        // ... other IEmailSender methods ...
    }
    

    Then just query SendCount after making your method call, and making sure that it’s == 1.

    Remember, Rhino Mocks is creating this dynamically for you — if you do it manually you have to react to interface changes before compile time, by hand.

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

Sidebar

Related Questions

Typically languages have keywords that you are unable to use directly with the exact
So typically I just use stopPropegation when I need to prevent the default action
I typically use the site http://www.pinvoke.net/ to grab a DllImport declaration whenever I need
I need to implement some low-level IP filtering/tracking. Typically I would use iptables for
When developing for Android do you typically need to stop the emulator and restart
I need to simulate multiple embedded server devices that are typically used for motor
Typically I develop my websites on trunk, then merge changes to a testing branch
Typically I use E_ALL to see anything that PHP might say about my code
Typically I create a plugin when I have a module that I know I'm
Typically, when I use lambdas, I just use a, b, c, d... as variable

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.