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