You don't need to worry about what gets passed in to your dependency,
you just want it to throw an exception.  To make the test easier, I
would change your stub to:

stub.Stub(s => s.InsertIntoInbox(null)).IgnoreArguments().Throw(new
Exception("Error"));

Also, I assume ISMLSMSD is an interface?

---
Patrick Steele
http://weblogs.asp.net/psteele



On Wed, Dec 22, 2010 at 6:56 AM, Jesper Lund Stocholm
<[email protected]> wrote:
> I have a repository (InboxRepository) with a dependency of type
> ISMLSMSD.
>
> I would like to test that the repository behaves correctly when the
> dependency throws an exception in one of the methods.
>
> The InboxRepository has these two overloaded methods:
>
> public void InsertInboxMessage(IInboxItem inboxItem)
> {
>    // Simply call overloaded method
>    InsertInboxMessage(new List<IInboxItem>() { inboxItem });
> }
>
> public void InsertInboxMessage(List<IInboxItem> inboxItems)
> {
>    try
>    {
>        _sMLSMSD.InsertIntoInbox(inboxItems);
>    }
>    catch (Exception exception)
>    {
>
>        throw new InboxRepositoryException("InsertInboxMessage
> failed", exception);
>    }
> }
>
> The dependency has this method:
>
> public int InsertIntoInbox(List<IInboxItem> inboxMessages)
>
> So I would like to test that my repo wraps an Exception from
> dependency in its own specialized InboxRepositoryException.
>
> So I wrote this test:
>
> [Test]
> public void CanWrapExceptionInInsertListInboxMessageTest()
> {
>    var stub = MockRepository.GenerateStub<ISMLSMSD>();
>    var implementation = new InboxRepository(stub);
>
>    stub.Stub(s => s.InsertIntoInbox(new List<IInboxItem>() { new
> InboxMessage() })).Throw(new Exception("Error"));
>
>    Assert.Throws<InboxRepositoryException>(() =>
> implementation.InsertInboxMessage(new List<IInboxItem>() { new
> InboxMessage() }));
> }
>
> I would assume that the dependency would throw an Exception(“Error”)
> and that the exception thrown from repo was the wrapped one.
>
> However – that is not what happens. My test fails with this message:
>
>  Expected: <InboxRepositoryException>
>  But was:  null
>
> It seems to me that the Exception is never thrown in the generated
> dependency proxy.
>
> Can you help me here to what I’m missing?
>
> Thanks,
>
> :o)
>
> Med venlig hilsen / Best regards
>
> Jesper Lund Stocholm
>
> --
> 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.
>
>

-- 
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