Rhino MocksExamples from the real world

time to read 12 min | 2323 words

From the WatiN mailing list, using Rhino Mocks to verify that exceptions originating from IE won't fail the test:

[Test]

public void ExceptionsInDialogHandlersShouldBeLoggedAndNeglected()

{

   MockRepository mocks = new MockRepository();

 

   ILogWriter mockLogWriter = (ILogWriter) mocks.CreateMock(typeof (ILogWriter));

   IDialogHandler throwExceptionDialogHandler = (IDialogHandler) mocks.CreateMock(typeof (IDialogHandler));

   IDialogHandler simpleDialogHandler = (IDialogHandler) mocks.CreateMock(typeof (IDialogHandler));

   Window dialog = (Window) mocks.DynamicMock(typeof (Window), IntPtr.Zero);

 

   Expect.Call(dialog.IsDialog()).Return(true);

   Expect.Call(dialog.Visible).Return(true);

   Expect.Call(throwExceptionDialogHandler.HandleDialog(dialog)).Throw(new Exception());

   mockLogWriter.LogAction("");

   LastCall.Constraints(Text.Like("Exception was thrown while DialogWatcher called HandleDialog:"));

   mockLogWriter.LogAction("");

   LastCall.Constraints(Text.StartsWith("System.Exception:"));

   Expect.Call(simpleDialogHandler.HandleDialog(dialog)).Return(true);

 

   mocks.ReplayAll();

 

   Logger.LogWriter = mockLogWriter;

 

   DialogWatcher dialogWatcher = new DialogWatcher(0);

   dialogWatcher.Add(throwExceptionDialogHandler);

   dialogWatcher.Add(simpleDialogHandler);

 

   dialogWatcher.HandleWindow(dialog);

 

   mocks.VerifyAll();

}

More posts in "Rhino Mocks" series:

  1. (30 Jun 2008) Getting closer to conclusion
  2. (29 Jun 2008) The role of Stub vs. Mock
  3. (29 Jun 2008) To be strict or not?