Reputation: 33
I have some problem with my tests for application ASP.NET MVC 5 like this
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.
I tested UserService and I were using Moq and NUnit frameworks. I sovled all problems when I added this:
var mockContext = new Mock<SomeContext>() { CallBase = true };
but now I have this problem:
Message: System.ArgumentException : Constructor arguments cannot be passed for interface mocks.
If you want you can check my code below but I think problem only in "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);
}
Upvotes: 3
Views: 5780
Reputation: 4418
I guess problem is in this line:
var mock = new Mock<IIdentityUnitOfWork>(mockContext.Object);
Dynamic class created by moq based on IIdentityUnitOfWork has only default constructor and it does not know what to do with mockContext.Object. Removing mockContext.Object from mock constructor should fix your problem.
Upvotes: 2