我在使用Moq库在项目中模拟枚举时遇到问题。我正在尝试测试一个类,其中一个方法接受一个ENum。有什么办法吗?
这是我要模拟的枚举:
public enum PermissionType
{
Create = 0,
Read = 1,
Update = 2,
Delete = 3,
}
这是我试图用来创建模拟的代码:
private static Mock<PermissionService> GetMockPermissionService(bool hasPermissions)
{
var mockPermissionService = new Mock<PermissionService>();
mockPermissionService.Setup(x => x.HasPermission(It.IsAny<string>(),
**It.IsAny<PermissionType>()**)).Returns(hasPermissions);
return mockPermissionService;
}
这是我收到的错误:
我也尝试过:
mockPermissionService.Setup(x => x.HasPermission(It.IsAny<string>(),
**It.IsAny<int>()**)).Returns(hasPermissions);
mockPermissionService.Setup(x => x.HasPermission(It.IsAny<string>(),
**PermissionType.Read**)).Returns(hasPermissions);
任何帮助,将不胜感激...
最佳答案
此错误意味着您在PermissionService上的HasPermission方法必须是虚拟的,如下所示:
public virtual bool HasPermission(string name, PermissionType type)
{
// logic
}