我在使用Moq库在项目中模拟枚举时遇到问题。我正在尝试测试一个类,其中一个方法接受一个ENum。有没有办法做到这一点?
以下是我试图嘲笑的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;
}
这是我收到的错误:
System.ArgumentException:无效 在不可覆盖的成员上设置:x =&GT; x.HasPermission(It.IsAny(),It.IsAny())
我也尝试过:
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);
任何帮助将不胜感激......
答案 0 :(得分:3)
此错误意味着PermissionService上的HasPermission方法必须是虚拟的,如下所示:
public virtual bool HasPermission(string name, PermissionType type)
{
// logic
}