问题描述
我需要您的帮助,以便在 .net Core 控制台应用程序中使用单元测试方法来处理 Moq。 道歉,如果有人问过这个问题,但我尝试过但找不到答案。
有三个实现一个接口的类
public class MailNotification : ISendNotification
{
public bool SendNotification()
{
return true;
}
}
public class EmailNotification : ISendNotification
{
public bool SendNotification()
{
return true;
}
}
public class SmsNotification : ISendNotification
{
public bool SendNotification()
{
return true;
}
}
在 Program.cs 文件中,我们有:
private static IServiceCollection ConfigureServices()
{
IServiceCollection services = new ServiceCollection();
var config = LoadConfiguration();
services.AddSingleton(config);
services.AddTransient<IUser,User>();
services.AddTransient<Something>();
services.AddTransient<MailNotification>();
services.AddTransient<EmailNotification>();
services.AddTransient<SmsNotification>();
//multiply concrete implementation of an Interface
services.AddTransient<Func<string,ISendNotification>>(serviceProvider => key =>
{
switch (key)
{
case "Mail":
return serviceProvider.GetService<MailNotification>();
case "Email":
return serviceProvider.GetService<EmailNotification>();
default:
return serviceProvider.GetService<SmsNotification>();
}
});
return services;
}
类看起来像这样:
public class Something
{
private readonly IConfiguration config;
private readonly IUser user;
private readonly Func<string,ISendNotification> sendMsg;
public Something(IConfiguration config,IUser user,Func<string,ISendNotification> send)
{
this.config = config;
this.user = user;
this.sendMsg = send;
}
public bool ProcessUser()
{
bool result;
switch (user.PreferredCommunication.ToString())
{
case "Mail":
result = sendMsg(NotificationType.Mail.ToString()).SendNotification();
break;
case "Email":
result = sendMsg(NotificationType.Email.ToString()).SendNotification();
break;
default:
result = sendMsg (NotificationType.SMS.ToString()).SendNotification();
break;
}
return result;
}
}
这是单元测试类
public class UnitTest1
{
private readonly ITestOutputHelper outPutHelper;
private readonly IConfiguration config;
public UnitTest1(ITestOutputHelper helper)
{
this.outPutHelper = helper;
//get path to appsettings file,assembly location
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
string projectPath = Path.GetDirectoryName(path);
config = new ConfigurationBuilder().SetBasePath(projectPath).AddJsonFile("appsettings.json").Build();
}
[Fact]
[Trait("Category","Unit")]
public void ProcessUser_MailNotification_True()
{
//Arrange
Mock<ISendNotification> mockNotify = new Mock<ISendNotification>();
mockNotify.Setup(x => x.SendNotification()).Returns(true);
Mock<IUser> mockUser = new Mock<IUser>();
mockUser.Setup(x => x.TruncateName(It.IsAny<string>()));
Func<string,ISendNotification> func = () => {
return new Mock<"Mail",ISendNotification>();
}; //< -- help help here
//The error is Delegate'Func<string,ISendNotification>' does not take 0 arguments
//Act
var sut = new Something(config,mockUser.Object,mockNotify.Object); //< -- help help here
//The error is Arugment 3: cannot convert from 'ISendNotification' to System.Func<string,ISendNotification>'
}
}
非常感谢您的帮助!
解决方法
未正确声明委托
//...
Func<string,ISendNotification> send = (string key) => mockNotify.Object;
//...
并且委托也是需要传递给被测对象的东西
//...
var sut = new Something(config,mockUser.Object,send);
//...
从那里可以进行测试来断言预期的行为
//...
//Act
bool actual = sut.ProcessUser();
//Assert - FluentAssertions
actual.Should().BeTrue();
但基于被测成员中使用的依赖项,
//...
switch (user.PreferredCommunication.ToString())
//...
需要进一步设置以允许被测成员完成。但是由于原始问题中没有提供该细节,我将无法指定该值应该是什么。