title: Raising events from a mock/stub using Rhino Mocks
body: <p>How can I raise an event from a mock/stub using Rhino Mocks?</p>
  
  <p>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.</p>
  
  <p>Any suggestions?</p>
  
  <p>A little example...</p>
  
  <p>Let's say I'm using the MVVM pattern and have this model class:</p>
  
  <pre><code>public class MyModel
  {
      private int _myValue;
  
      public event EventHandler ValueChanged;
  
      public void SetValue(int newValue)
      {
          _myValue = newValue;
          if (ValueChanged != null)
          {
              ValueChanged(this, null);
          }
      }
  }
  </code></pre>
  
  <p>As you can see the class has an integer value and a method that sets it. When the value is set, a <code>ValueChanged</code> event is raised.</p>
  
  <p>This model class is used by a viewmodel:</p>
  
  <pre><code>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; }
  }
  </code></pre>
  
  <p>This viewmodel listen to the <code>ValueChanged</code> event on the model and updates when it is raised.</p>
  
  <p>Now I want to test that the viewmodel is updated when the model  raises the event.</p>
  
  <pre><code>[TestMethod]
  public void MyViewModel_ModelRaisesValueChangedEvent_MyStringIsUpdated()
  {
      //Arrange.
      var modelStub = MockRepository.GenerateStub&lt;MyModel&gt;();
      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);
  }
  </code></pre>
  
  <p>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.</p>