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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:57:05+00:00 2026-05-14T15:57:05+00:00

I am new to moq and I was trying to test a controller (MVC)

  • 0

I am new to moq and I was trying to test a controller (MVC) behaviour that when the view raises a certain event, controller calls a certain function on model, here are the classes –

public class Model
{
    public void CalculateAverage()
    {
        ...
    }

    ...
}

public class View
{
    public event EventHandler CalculateAverage;

    private void RaiseCalculateAverage()
    {
        if (CalculateAverage != null)
        {
            CalculateAverage(this, EventArgs.Empty);
        }
    }

    ...
}

public class Controller
{
    private Model model;
    private View view;

    public Controller(Model model, View view)
    {
        this.model = model
        this.view = view;
        view.CalculaeAverage += view_CalculateAverage;
    }

    priavate void view_CalculateAverage(object sender, EventArgs args)
    {
        model.CalculateAverage();
    }
}

and the test –

[Test]
public void ModelCalculateAverageCalled()
{
    Mock<Model> modelMock = new Mock<Model>();
    Mock<View> viewMock = new Mock<View>();
    Controller controller = new Controller(modelMock.Object, viewMock.Object);
    viewMock.Raise(x => x.CalculateAverage += null, new EventArgs.Empty);
    modelMock.Verify(x => x.CalculateAverage());
    //never comes here, test fails in above line and exits
    Assert.True(true);
}

The issue is that the test is failing in the second last line with “Invocation was not performed on the mock: x => x.CalculateAverage()”. Another thing I noticed is that the test terminates on this second last line and the last line is never executed. Am I doing everything correct?

  • 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-14T15:57:05+00:00Added an answer on May 14, 2026 at 3:57 pm

    Please find below working snippet. The difference is that “CalculateAverage” is declared as virtual. The reason is that Moq creates runtime wrapper over “Mock” classes and overrides its beheviour. But if method is not virtual, then it is just impossible.

        public class Model
        {
            public virtual void CalculateAverage()
            {
            }
        }
    
        public class View
        {
            public virtual event EventHandler CalculateAverage;
        }
    
        public class Controller
        {
            private Model model;
            private View view;
    
            public Controller(Model model, View view)
            {
                this.model = model;
                this.view = view;
                view.CalculateAverage += view_CalculateAverage;
            }
    
            private void view_CalculateAverage(object sender, EventArgs args)
            {
                model.CalculateAverage();
            }
        }
    
        [TestFixture]
        public class MvcTest
        {
            [Test]
            public void ModelCalculateAverageCalled()
            {
                var modelMock = new Mock<Model>();
                var viewMock = new Mock<View>();
    
                var controller = new Controller(modelMock.Object, viewMock.Object);
    
                viewMock.Raise(x => x.CalculateAverage += null, EventArgs.Empty);
                modelMock.Verify(x => x.CalculateAverage());
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a test for an ASP.Net MVC controller action. I'd like
I'm trying to test/spec the following action method public virtual ActionResult ChangePassword(ChangePasswordModel model) {
I am trying to test that a property has been set but when I
I'm trying to set up some Moq repositories to test my service with Castle
I am new with Moq and TDD and what I am trying to do
I am new to mocking and trying out moq for the first time. I
i've got the following Action Method I'm trying to moq test. Notice the AcceptVerbs
I am trying to learn TDD/BDD using NUnit and Moq. The design that I
Trying to use NUnit to test a method that adds an object to a
I am trying to write a unit test for an Action who's model uses

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.