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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:34:59+00:00 2026-05-17T23:34:59+00:00

How can I raise an event from a mock/stub using Rhino Mocks? I’ve found

  • 0

How can I raise an event from a mock/stub using Rhino Mocks?

I’ve found some answers to this question on the web, but they all seem to use the Record/Replay-syntax, but I’m using the Arrange/Act/Assert syntax.

Any suggestions?

A little example…

Let’s say I’m using the MVVM pattern and have this model class:

public class MyModel
{
    private int _myValue;

    public event EventHandler ValueChanged;

    public void SetValue(int newValue)
    {
        _myValue = newValue;
        if (ValueChanged != null)
        {
            ValueChanged(this, null);
        }
    }
}

As you can see the class has an integer value and a method that sets it. When the value is set, a ValueChanged event is raised.

This model class is used by a viewmodel:

public class MyViewModel
{
    private MyModel _myModel;

    public MyViewModel(MyModel myModel)
    {
        _myModel = myModel;
        _myModel.ValueChanged += ValueChangedHandler;
    }

    private void ValueChangedHandler(object sender, EventArgs e)
    {
        MyString = "Value has changed";
    }

    public string MyString { get; set; }
}

This viewmodel listen to the ValueChanged event on the model and updates when it is raised.

Now I want to test that the viewmodel is updated when the model raises the event.

[TestMethod]
public void MyViewModel_ModelRaisesValueChangedEvent_MyStringIsUpdated()
{
    //Arrange.
    var modelStub = MockRepository.GenerateStub<MyModel>();
    MyViewModel viewModel = new MyViewModel(modelStub);

    //Act
    -HERE I WANT TO RAISE THE VALUE CHANGED EVENT FROM modelStub.

    //Assert.
    Assert.AreEqual("Value has changed", viewModel.MyString);
}

Note that this is just an example and not my actual code (which is more complex). I hope you can disregard any typos and other small mistakes.

  • 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-17T23:35:00+00:00Added an answer on May 17, 2026 at 11:35 pm
    [TestMethod]
    public void MyViewModel_ModelRaisesValueChangedEvent_MyStringIsUpdated()
    {
        //Arrange.
        var modelStub = MockRepository.GenerateStub<IModel>();
        MyViewModel viewModel = new MyViewModel(modelStub);
    
        //Act
        modelStub.Raise(
           x => x.ValueChanged += null,
           modelStub, // sender
           EventArgs.Empty);
    
        //Assert.
        Assert.AreEqual("Value has changed", viewModel.MyString);
    }
    

    Edit: The error you are encountering can probably be solved by changing the stub type to an interface IModel (that’s how my own tests work). I have changed it in example above, but you will also have to change the MyViewModel constructor declaration to take the interface type.

    Adding the virtual keyword to the event declaration may also work.

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

Sidebar

Related Questions

How can I raise DataObject.Pasting event from my code?
I want to raise an event from a .NET class, and receive this event
I understand that one can raise an event in the class that the implementation
I am trying to raise some custom events to a SqlWebEventProvider from a windows
How do I raise an event from a user control that was created dynamically?
Quote from: http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx "Invoking an event can only be done from within the class
How to raise a event in usercontrol from the another usercontrol. i tried to
Is there a good way to find out which exceptions a procedure/function can raise
can you recommend some good ASP.NET tutorials or a good book? Should I jump
I need to solve the following question which i can't get to work by

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.