Reputation: 951
Can anyone help with this following code, which is failing. From what I can see I can't tell why the expected params I'm listing will not cause the method to fire.
Test code:
Mock<ExpiryNotifier> target = new Mock<ExpiryNotifier>();
Mock<MailServiceWrapper> mailMock = new Mock<MailServiceWrapper>();
mailMock.Verify(
m => m.SendMail(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string[]>(),
It.IsAny<string[]>(),
It.IsAny<string[]>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string[]>()
),
Times.Exactly(1)
);
target.Setup(t => t.getMailService()).Returns(mailMock.Object);
target.Object.notify();
The actual code:
public virtual MailServiceWrapper getMailService()
{
MailServiceWrapper MailService = new MailServiceWrapper();
return MailService;
}
public string notify()
{
string feed = loadFeed();
MailServiceWrapper MailService = getMailService();
MailService.SendMail(
"sysmail.blah.net",
"[email protected]",
new string[] {"[email protected]"},
new string[] { },
new string[] { },
"blah blah",
"This is a blah blah email",
new string[] { }
);
}
method signature:
public virtual string SendMail(string server, string from, string[] to, string[] ccs, string[] bccs, string title, string body, string[] attachments)
Upvotes: 0
Views: 55
Reputation: 3601
You need to verify/assert after you've executed your test:
// Arrange
Mock<ExpiryNotifier> target = new Mock<ExpiryNotifier>();
Mock<MailServiceWrapper> mailMock = new Mock<MailServiceWrapper>();
target.Setup(t => t.getMailService()).Returns(mailMock.Object);
// Act
target.Object.notify();
// Assert
mailMock.Verify(
m => m.SendMail(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string[]>(),
It.IsAny<string[]>(),
It.IsAny<string[]>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string[]>()
),
Times.Exactly(1)
);
Upvotes: 3
Reputation: 1449
.Verify()
is a method that's called after the fact in order to, indeed, verify that the method you're suggesting was called.
Upvotes: 0