gpt4 book ai didi

asp.net-mvc - Moq 表示不能为接口(interface)模拟传递构造函数参数

转载 作者:行者123 更新时间:2023-12-04 03:09:54 27 4
gpt4 key购买 nike

我对像这样的应用程序 ASP.NET MVC 5 的测试有一些问题

Castle.Proxies.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. Castle.Proxies.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

我测试了 UserService 并使用了 Moq 和 NUnit 框架。当我添加这个时,我解决了所有问题:

var mockContext = new Mock<SomeContext>() { CallBase = true };

但是现在我遇到了这个问题:

Message: System.ArgumentException : Constructor arguments cannot be passed for interface mocks.

如果你愿意,你可以检查我下面的代码,但我认为问题只在“mockContext”中

[Test, TestCaseSource(typeof(UserServiceTestData), nameof(UserServiceTestData.WrongCreateUserTestCases))]
public void ShouldWrongUserCreationTest(UserDTO userDto)
{
//Arrange
var user = new ApplicationUser
{
Email = "test",
Roles = { new IdentityUserRole { UserId = "test", RoleId = "test" } },
ClientProfile = new ClientProfile { Name = "test" }
};

var mockStore = new Mock<IUserStore<ApplicationUser>>();
mockStore.Setup(x => x.FindByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(user));
mockStore.As<IUserEmailStore<ApplicationUser>>().Setup(x => x.FindByEmailAsync("existed")).Returns(Task.FromResult((ApplicationUser)null));
mockStore.As<IUserPasswordStore<ApplicationUser>>();
mockStore.As<IUserRoleStore<ApplicationUser>>().Setup(x => x.AddToRoleAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
.Returns(Task.FromResult(IdentityResult.Success));
mockStore.As<IUserRoleStore<ApplicationUser>>().Setup(x => x.GetRolesAsync(It.IsAny<ApplicationUser>()))
.Returns(Task.FromResult((IList<string>)new List<string>()));
mockStore.Setup(x => x.CreateAsync(It.IsAny<ApplicationUser>()))
.Returns(Task.FromResult(IdentityResult.Success));

var mockDbSet = new Mock<DbSet<ClientProfile>>(MockBehavior.Strict);
mockDbSet.Setup(x => x.Add(It.IsAny<ClientProfile>())).Returns(new ClientProfile());

var userManager = new ApplicationUserManager(mockStore.Object);
var mockContext = new Mock<AuthenticationContext>() { CallBase = true };
var mock = new Mock<IIdentityUnitOfWork>(mockContext.Object);
mock.Setup(a => a.UserManager).Returns(userManager);

//Act
var userService = new UserService(mock.Object);
OperationDetails result = userService.Create(userDto);

//Assert
Assert.That(result.Succedeed, Is.False);
}

最佳答案

我想问题出在这一行:

var mock = new Mock<IIdentityUnitOfWork>(mockContext.Object);

moq 基于 IIdentityUnitOfWork 创建的动态类只有默认构造函数,不知道如何处理 mockContext.Object。从模拟构造函数中删除 mockContext.Object 应该可以解决您的问题。

关于asp.net-mvc - Moq 表示不能为接口(interface)模拟传递构造函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46066587/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com