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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:31:54+00:00 2026-05-26T18:31:54+00:00

I was testing a model repository to see if it calls the message bus.

  • 0

I was testing a model repository to see if it calls the message bus. I am not sure if this is a good test at all but here is my thinking: I would normally put the bus.send into the controller (this is an MVC web app) but since I don’t want to test my controllers specifically for logic, I moved this into the repository. Controllers are simple in my case. Repository uses the bus and the model database to build the view models.

Anyways, point of this problem is the moq test I am running. I mocked the bus and wanted to verify that it is called from the repository.

The test looks like this:

public class when_creating_new_clinic
{
    Establish context = () =>
    {
        clinicID = Guid.NewGuid();
        model = new ClinicModel
        {
            ClinicID = clinicID,
            Alias = "alias",
            Title = "title"
            // stuff omitted
        };
        newClinicData = new NewClinicData
        {
            ClinicID = clinicID,
            Alias = "alias",
            Title = "title"
            // stuff omitted 
        };
        cmd = new CreateClinicCmd(newClinicData);
        bus = new Mock<IMessageBusAgent>();
        repository = new ClinicModelRepository(bus.Object);

        bus.Setup(b => b.Send(cmd));
    };

    Because it = () => repository.Create(model);

    It should_send_create_clinic_command_to_bus = () =>
    {
        bus.Verify(b => b.Send(cmd), Times.Exactly(1));
    };

    static ClinicModelRepository repository;
    static ClinicModel model;
    static Mock<IMessageBusAgent> bus;
    static NewClinicData newClinicData;
    static Guid clinicID;
    static CreateClinicCmd cmd;
}

The gist of the repository is this:

public class ClinicModelRepository : IClinicModelRepository
{
    private readonly IMessageBusAgent m_bus;

    public ClinicModelRepository(IMessageBusAgent bus)
        : this()
    {
        m_bus = bus;
    }

    public void Create(ClinicModel clinicModel)
    {
        // stuff omitted (data is mapped from clinicModel)          

        m_bus.Send(new CreateClinicCmd(data));
    }
}

The IMessageBusAgent is declared as:

public interface IMessageBusAgent : IDomainCommandSender, IDomainEventPublisher, IUnitOfWork
{
}

The result of the test looks like this:

when creating new clinic

» should send create clinic command to bus (FAIL)

Test ‘should send create clinic command to bus’ failed:
Moq.MockException:
Expected invocation on the mock exactly 1 times, but was 0 times: b => b.Send(when_creating_new_clinic.cmd)

Configured setups:
b => b.Send<CreateClinicCmd>(when_creating_new_clinic.cmd), Times.Never

Performed invocations:
IDomainCommandSender.Send(ArReg.Commands.CreateClinicCmd)
IUnitOfWork.Commit()
at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable`1 setups, IEnumerable`1 actualCalls, Expression expression, Times times, Int32 callCount)
at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
at Moq.Mock.Verify[T](Mock mock, Expression`1 expression, Times times, String failMessage)
at Moq.Mock`1.Verify(Expression`1 expression, Times times)
Repositories\when_creating_new_clinic.cs(51,0): at ArReg.Tests.Specs.Repositories.when_creating_new_clinic.<.ctor>b__4()
at Machine.Specifications.Model.Specification.InvokeSpecificationField()
at Machine.Specifications.Model.Specification.Verify()

0 passed, 1 failed, 0 skipped, took 3.58 seconds (Machine.Specifications 0.4.24-f7fb6b5).

The Send() command is declared in the IDomainCommandSender so how do I need to setup the test so that I can verify the correct call?

Thanks

  • 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-26T18:31:55+00:00Added an answer on May 26, 2026 at 6:31 pm

    Your setup of the bus-moq has a little mistake. It should be like this:

    bus.Setup(b => b.Send(It.IsAny<CreateClinicCmd>()));
    

    Reason: You wrote your setup with an instance of CreateClinicCmd created two code lines above. In your class under test ClinicModelRepository you create another instance of that class and call your bus mock. This call doesn’t match the call you wrote in your setup.

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

Sidebar

Related Questions

Unit testing sounds great to me, but I'm not sure I should spend any
Testing: return request.getCookies() == null; is not an appropriate way test. Is there another
I have this in my controller public ActionResult Testing() { CustomerContactModel model = new
I have been testing inline function calls in C++. Thread model: win32 gcc version
In testing a getter/setter pair in a rails model, I've found a good example
My controller has this (testing) code: println domainInstance.hasErrors() render (view: edit, model: [domainInstance: domainInstance])
There's many examples of testing RoR model validations. Even keeping the test being DRY
I understand that in the V model testing there is a corresponding test for
Unit testing is, roughly speaking, testing bits of your code in isolation with test
Unit testing with C/C++: What do you teach people who either did not do

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.