Thanks Patrick.

This worked great!  I also tried out using the static
MockRepository.XXX methods and came up with an alternate solution
(below).

I've been quite awash in the documentation giving several different
ways to accomplish things.  It seems that using the Static methods is
provided as convenience syntax for common cases.  I think I prefer
your solution of the explicit record/replay as it's more readable in
this case.

     var mockScriptProc =
MockRepository.GenerateStub<ScriptProcessor>();
     target.scriptProc = mockScriptProc;
     // define stub behavior so that RunScriptEngine always raises
event with actual handler as the handler
     mockScriptProc.Stub(sp => sp.RunScriptEngine(null, null, null))
           .IgnoreArguments()
           .Do(new RunEngineDelegate((a, b, c) =>
                 mockScriptProc.Raise(delegate
{ target.scriptProc.OnScriptingError +=
target.HandleScriptProcessingError; } ,
                 null, 55, "desc")));
     mockScriptProc.DoSomething();

On Jul 13, 7:48 am, Patrick Steele <[email protected]> wrote:
> I played around with this a bit (had to create some stand-in classes
> for your actual classes), but I think I see what happened.
>
> When creating your own MockRepository (vs. using the static
> MockRepository.XXX methods), you need to make sure you set your
> record/playback modes properly.  Here's an example that I think is
> what you were after (for easier reading, you can view the sample here
> too:http://pastebin.com/bCJqPMQH)
>
> using Microsoft.VisualStudio.TestTools.UnitTesting;
> using Rhino.Mocks;
> using Rhino.Mocks.Interfaces;
>
> namespace TestProject1
> {
>         public delegate void ScriptProcessingErrorHandler(object sender, int
> lineNumber, string description);
>
>         [TestClass]
>         public class RaisingEventFromTest
>         {
>                 /// <summary>
>                 ///Gets or sets the test context which provides
>                 ///information about and functionality for the current test 
> run.
>                 ///</summary>
>                 public TestContext TestContext { get; set; }
>
>                 [TestMethod]
>                 public void RaiseEventFromUnitTestMock()
>                 {
>                         var target = new Target();
>                         var mocks = new MockRepository();
>                         ScriptProcessor mockScriptProc = 
> mocks.DynamicMock<ScriptProcessor>();
>
>                         using (mocks.Record())
>                         {
>                                 mockScriptProc.OnScriptingError += null;
>                                 LastCall.IgnoreArguments();
>                                 IEventRaiser spRaiser = 
> LastCall.GetEventRaiser();
>
>                                 // define stub behavior so that 
> RunScriptEngine always raises
> event with actual handler as the handler
>                                 mockScriptProc.Stub(sp => 
> sp.RunScriptEngine(null, null, null))
>                                         .IgnoreArguments()
>                                         .Do(new RunEngineDelegate((a, b, c) 
> => spRaiser.Raise(null, 55, "desc")));
>
>                                 target.scriptProc = mockScriptProc;
>                         }
>
>                         // Here I want to call a function which invokes 
> RunScriptEngine and
> verify that
>                         // it handles the events
>                         using (mocks.Playback())
>                         {
>                                 mockScriptProc.OnScriptingError += 
> target.HandleScriptProcessingError;
>                                 mockScriptProc.DoSomething();
>                         }
>                 }
>
>                 private delegate void RunEngineDelegate(object a, object b, 
> object c);
>         }
>
>         public class ScriptProcessor
>         {
>                 public virtual event ScriptProcessingErrorHandler 
> OnScriptingError;
>
>                 public virtual void RunScriptEngine(object a, object b, 
> object c)
>                 {
>
>                 }
>
>                 public void DoSomething()
>                 {
>                         RunScriptEngine(null, null, null);
>                 }
>         }
>
>         public class Target
>         {
>                 public ScriptProcessor scriptProc { get; set; }
>
>                 public void HandleScriptProcessingError(object sender, int
> lineNumber, string description)
>                 {
>                         var check = lineNumber.ToString();
>                 }
>         }
>
> }
>
> Let me know if this works for you.  Thanks.
>
> ---
> Patrick Steelehttp://weblogs.asp.net/psteele
>
> On Mon, Jul 12, 2010 at 8:46 PM, Kim Stewart <[email protected]> wrote:
> > I'm new to Rhino.Mocks and mocking in general, and I've been trying to
> > work out a case in which I want to test that my class responds
> > properly to a (non System.Event) event, so I want to assign the event
> > to be raised by the actual handler.
>
> > In this case, the class owns the mocked object.  I want to force the
> > mock to fire the event every time a particular function of the mock is
> > invoked.  However, I get run time errors when I attempt to raise the
> > event.
>
> > Application code:
>
> > public delegate void ScriptProcessingErrorHandler(object sender, int
> > lineNumber, string description);
> > public virtual event ScriptProcessingErrorHandler OnScriptingError;
>
> > Here's the relevant test code:
> >            ScriptProcessor mockScriptProc =
> > mocks.DynamicMock<ScriptProcessor>();
> >            mockScriptProc.OnScriptingError +=
> > target.HandleScriptProcessingError;
> >            LastCall.IgnoreArguments();
> >            IEventRaiser spRaiser = LastCall.GetEventRaiser();
> >            // define stub behavior so that RunScriptEngine always
> > raises event with actual handler as the handler
> >            mockScriptProc.Stub(sp =>
> > mockScriptProc.RunScriptEngine(null, null, null)).IgnoreArguments()
> >                .Raise(s => spRaiser.Raise()); // this is line 468
> >            target.scriptProc = mockScriptProc;
> >            // Here I want to call a function which invokes
> > RunScriptEngine and verify that
> >            // it handles the events
>
> > And the stack trace
> > Rhino.Mocks.MockRepository.GetMockedObject(Object mockedInstance)
> > R](T mock, Function`2 action)
> > R](T mock, Function`2 action)
> > Rhino.Mocks.RhinoMocksExtensions.Stub[T](T mock, Action`1 action)
> > Rhino.Mocks.RhinoMocksExtensions.GetEventRaiser[TEventSource]
> > (TEventSource mockObject, Action`1 eventSubscription)
> > Rhino.Mocks.RhinoMocksExtensions.Raise[TEventSource](TEventSource
> > mockObject, Action`1 eventSubscription, Object[] args)
> >  . . ScriptErrorUsingRhinoMocksStubTest() in C:\. . .
> > MyClassPath.. . .MyTest.cs: line 468
>
> > Here's some working code . . . but I have to manually raise the
> > event.  Currently this is okay, because the event sets flags which the
> > SetAmplitude function checks, but it won't be okay for the future when
> > the event handling is fleshed out.
>
> >            MockRepository mocks = new MockRepository();
> >            ScriptProcessor mockScriptProc =
> > mocks.DynamicMock<ScriptProcessor>();
> >            target.scriptProc = mockScriptProc;
> >            IEventRaiser scriptProcEvent = Expect.Call(delegate
> > { target.scriptProc.OnScriptingError +=
>
> > target.HandleScriptProcessingError; }).IgnoreArguments().GetEventRaiser();
> >            mocks.ReplayAll();
> >            scriptProcEvent.Raise(target.scriptProc, 0, "Test
> > Description");
> >            // Here I call the function which checks the flags that
> > the event handler set
> >            target.SetAmplitude(2, expectedAmp);
> >            // State verification works after this point
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Rhino.Mocks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/rhinomocks?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to