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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:08:40+00:00 2026-05-13T00:08:40+00:00

In a particular unit test I’m trying to raise an event multiple times and

  • 0

In a particular unit test I’m trying to raise an event multiple times and then to veryify a property value after the final event has been raised. I have something like

public void TurnRight()
{
   var mockFoo = new Mock<IFoo>();

   SomeService someService= new SomeService ();
   someService.Foo= mockFoo.Object;

   mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty);
   mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty);
   mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty);

   mockFoo.VerifySet(foo=> foo.Orientation = Orientation.West);
}

Orientation actually only changed to east (as I believe the event is only getting raised once). Am I doing something wrong? This is the first time i’ve used moq so I’m probably missing something.

Cheers
J

edit… the correct code i should have been using

public void TurnRight()
    {
       var mockFoo = new Mock<IFoo>();

       SomeService someService= new SomeService ();
       someService.Foo= mockFoo.Object;

       mockFoo.SetupProperty(foo=> foo.Orientation);

       mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty);
       mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty);
       mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty);

       Assert.AreEqual(mockFoo.Object.Orientation, Orientation.South);
    }
  • 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-13T00:08:40+00:00Added an answer on May 13, 2026 at 12:08 am

    mockFoo.Raise should be fine, raising the event three times… Can you put a breakpoint in the event handler and check how many times is it called?

    Another potential mistake here as I can see, is that you should first tell Moq to start tracking all sets/gets of a property before you can verify it (and before you raise the events):

    // start "tracking" sets/gets to this property
    mockFoo.SetupProperty(foo=> foo.Orientation);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Occasionally I come accross a unit test that doesn't Assert anything. The particular example
Assume I want to write a unit test to test a particular piece of
I'm trying to write a unit test for a module that will give me
For a particular rails unit test(of type PerformanceTest), I need to GET a page
I'm new to unit testing and NUit in particular. I'm just typing some examples
Currently I'm writing a unit test for a component that does datetime specific validation.
I need to create a unit test for a method that returns an xmldocument.
I am a newbie to development and to unit tests in particular . I
I am writing unit tests against my ASP.NET MVC application, in particular I am
I've written a specialized HTML parser, that I want to unit test with a

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.