Assert.IsType<> 无法与 Moq 框架一起正常工作

Assert.IsType<> not working properly with Moq framework

尝试检查模拟对象的类型时出现以下错误:

 Assert.IsType() Failure
 Expected: ProjetoAxion.Domain.Entities.User
 Actual:   Castle.Proxies.UserProxy

示例:

var userMock = Mock<User>().Object;
Assert.IsType<User>(userMock);

我如何断言 Moq 的类型是模拟类型?

这就是 moq 内部工作的方式。它在后台使用 DynamicProxy 创建一个可拦截代理,其中代理 Castle.Proxies.UserProxy 扩展 User。否则,moq 将如何拦截调用...知道您可以使用 IsAssignableFrom<User> 而不是 IsType<User>

Assert.IsAssignableFrom<User>(userMock);