Asserting a mail message wasn't sent

6 views
Skip to first unread message

Goatified Creature

unread,
Nov 20, 2009, 6:19:38 AM11/20/09
to Rhino.Mocks
Hi,

Having embraced the AAA syntax, and one assert per unit test
principle, I have put some new tests into my current project. However,
I now face a problem where I am testing a method that looks up
objects and possibly sends emails off regarding them.

I have identified the following unit test:

[Test]
[Ignore]
public void should_not_send_email_to_invalid_address()
{
// arrange
IServiceAreaDAO serviceAreaDAO =
MockRepository.GenerateStub<IServiceAreaDAO>();
IEmailHelper emailHelper =
MockRepository.GenerateStub<IEmailHelper>();
ISmtpClient smtpClient =
MockRepository.GenerateMock<ISmtpClient>();

serviceAreaDAO.Stub(x => x.GetServiceAreaByName
(serviceAreaNames[0])).Return(serviceAreaA);
serviceAreaDAO.Stub(x => x.GetServiceAreaByName
(serviceAreaNames[1])).Return(serviceAreaB);
serviceAreaDAO.Stub(x => x.GetServiceAreaByName
(serviceAreaNames[2])).Return(serviceAreaC);

// act
SearchManager managerToTest = new SearchManager();
managerToTest.ServiceAreaDAO = serviceAreaDAO;
managerToTest.EmailHelper = emailHelper;
managerToTest.SmtpClient = smtpClient;

emailHelper.Stub(x => x.CreateArticleUpdateMailMessageTests
(Arg<String>.Is.Anything, Arg.Is(serviceAreaB.ExpertEmailAddress),
Arg<string>.Is.Anything, Arg<string>.Is.Anything,
Arg<string>.Is.Anything)).Throw(new System.FormatException());
smtpClient.Expect(x => x.Send(null)).IgnoreArguments();


try
{
managerToTest.MarkArticleForUpdate(null, null,
serviceAreaNames, null);
}
catch (ArticleUpdateEmailCouldNotBeSentException)
{
}


// assert
smtpClient.AssertWasNotCalled(x => x.Send
(Arg<MailMessage>.Matches(y => y.To[0].Equals
(serviceAreaB.ExpertEmailAddress))));
}

Here, I am trying to assert that an email message with the invalid
email address specified in serviceAreaB.ExpertEmailAddress will not be
sent. However, I get a:

TestCase
'LSC.DCCCSC.Business.Tests.SearchManagerTests.MarkArticleForUpdateTests.ServiceAreasWithInvalidEmailAddress.should_not_send_email_to_invalid_address'
failed: System.Reflection.TargetInvocationException : Exception has
been thrown by the target of an invocation.
----> System.NullReferenceException : Object reference not set to an
instance of an object.
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object
[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object
[] arguments, Signature sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at Rhino.Mocks.Constraints.LambdaConstraint.Eval(Object obj)
at Rhino.Mocks.Expectations.ConstraintsExpectation.DoIsExpected(Object
[] args)
at Rhino.Mocks.Expectations.AbstractExpectation.IsExpected(Object[]
args)
at Rhino.Mocks.RhinoMocksExtensions.AssertWasNotCalled[T](T mock,
Action`1 action, Action`1 setupConstraints)
at Rhino.Mocks.RhinoMocksExtensions.AssertWasNotCalled[T](T mock,
Action`1 action)
C:\Documents and Settings\psw\My Documents\Visual Studio 2008\Projects
\DCCCSC\LSC.DCCCSC.Business.Tests\SearchManagerTests
\MarkArticleForUpdateTests\ServiceAreasWithInvalidEmailAddress.cs
(98,0): at
LSC.DCCCSC.Business.Tests.SearchManagerTests.MarkArticleForUpdateTests.ServiceAreasWithInvalidEmailAddress.should_not_send_email_to_invalid_address
()
--NullReferenceException
at lambda_method(ExecutionScope , MailMessage )

Em.... I think the problem sits in the syntax of the
AssertWasNotCalled method, so i flipped it to asserted ServiceAreaA's
email was used, and it was, yet get the same exception....

Patrick Steele

unread,
Nov 20, 2009, 10:09:24 AM11/20/09
to rhino...@googlegroups.com
Syntax looks okay. I tried a small repo case and was able to get the
behavior you want (Rhino Mocks threw an exception about a call that
was made but should not have been).

MailMessage implements IDisposable. Is there a chance the MailMessage
has already been disposed of at the time you're trying to check if the
"To" collection contains an address?

--
Patrick Steele
http://weblogs.asp.net/psteele
Reply all
Reply to author
Forward
0 new messages