您需要 2 个测试来涵盖这一点,一个是Connect
返回true
,另一个是返回false
。
[Fact] // or [Test] depending on your testing framework
public void MailServiceConnectsIfCredentialsAreValid()
{
var mockMailService = new Mock<IMailService>();
mockMailService.Setup(x => x.Connect()).Returns(true);
// The rest of your code which will use the mockMailService.Object...
}
[Fact]
public void MailServiceFailsToConnectIfCredentialsAreInvalid()
{
var mockMailService = new Mock<IMailService>();
mockMailService.Setup(x => x.Connect()).Returns(false);
// The rest of your code which will use the mockMailService.Object...
}
出于测试的目的,您并不关心ICredentials
通过了什么。您只是涵盖了两种可能性,IMailService.Connect()
即它返回true
或false
。确定会发生什么的实际逻辑将存在于您的实际实现的测试中IMailService
。
您也可能会废弃该SetCredentials
方法,然后将其传递ICredentials
给Connect
.
bool Connect(ICredentials credentials);