【发布时间】:2016-10-26 13:04:10
【问题描述】:
我正在尝试从测试中抛出异常(使用 rhino 和 nunit),但它没有按预期工作
接口
public interface IEmailService
{
void SendEmail(string to, string subject, string body);
}
public interface IWebService
{
void LogError(string message);
}
类
public class MockService : IWebService
{
public string LastError;
public void LogError(string message)
{
LastError = message;
}
}
public class LogAnalyzerDynamicMockWithEmail
{
public IWebService Service { get; set; }
public IEmailService Email { get; set; }
public void Analyze(string fileName)
{
try
{
if (fileName.Length < 8)
Service.LogError("Filename too short:" + fileName);
}
catch (Exception exception)
{
Email.SendEmail("a", "subject", exception.Message);
}
}
}
这是测试方法
[Test]
public void Analyze_WebServiceThrows_SendsEmail()
{
var mockRepository = new MockRepository();
var stubWebService = mockRepository.Stub<IWebService>();
var mockEmailService = mockRepository.StrictMock<IEmailService>();
var logAnalyzerDynamicMockWithEmail = new LogAnalyzerDynamicMockWithEmail
{
Service = stubWebService,
Email = mockEmailService
};
stubWebService.Stub(m => m.LogError("whatever")).IgnoreArguments().Throw(new Exception("fake exception"));
Assert.Throws<Exception>(() => { logAnalyzerDynamicMockWithEmail.Analyze("abc.ext"); });
mockEmailService.AssertWasCalled(m => m.SendEmail("a", "subject", "fake exception"));
}
但是测试失败,消息是“Expected: <system.exception>, but was: null”
谁能帮我指出我在这里遗漏了什么?
【问题讨论】:
标签: c# unit-testing nunit rhino-mocks