asp.net MVC3单元测试 - 角色提供者

时间:2013-04-01 11:00:56

标签: asp.net-mvc asp.net-mvc-3

角色提供程序的单元测试失败。

[TestMethod]
    public void FindUsersInRole()
    {
        Mock<IUsersInRoleRepository> userInRoleMock = new Mock<IUsersInRoleRepository>();
        userInRoleMock.Setup(m => m.UsersInRoles).Returns(new UsersInRole[] {
            new UsersInRole { UserId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") },
            new UsersInRole { UserId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") },
            new UsersInRole { UserId = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"), RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") },
            new UsersInRole { UserId = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"), RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") },
            new UsersInRole { UserId = Guid.Parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"), RoleId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb") }
        }.AsQueryable());

        Mock<IRoleRepository> roleMock = new Mock<IRoleRepository>();
        roleMock.Setup(m => m.Roles).Returns(new Role[] {
            new Role { RoleId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), RoleName = "test" },
            new Role { RoleId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), RoleName = "admin" }
        }.AsQueryable());

        Mock<IUserRepository> userMock = new Mock<IUserRepository>();
        userMock.Setup(m => m.Users).Returns(new User[] {
            new User { UserId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), UserAccount = "abcdef" },
            new User { UserId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), UserAccount = "bcdef" },
            new User { UserId = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"), UserAccount = "cdef" },
            new User { UserId = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"), UserAccount = "bcdf" },
            new User { UserId = Guid.Parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"), UserAccount = "abcde" }
        }.AsQueryable());

        RoleProvider target = new RoleProvider(userMock.Object, roleMock.Object, userInRoleMock.Object);

        string[] result = target.FindUsersInRole("test", "cde");

        Assert.AreEqual(result[0], "abcdef");
        Assert.AreEqual(result[1], "bcdef");
        Assert.AreEqual(result[2], "cdef");
    }

单元测试代码

string [] result = target.FindUsersInRole(“test”,“cde”); &lt; - 错误

FindUsersInRole - 获取角色中的用户名数组,其中用户名包含要匹配的指定用户名。

引发System.NullReferenceException并尝试调试。

为什么NullReferenceException?

PS - FindUsersInRole(RoleProvider)

public override string[] FindUsersInRole(string roleName, string userAccountToMatch)
    {   
        Guid roleId = roleRepository.GetRole(roleName).RoleId; // RoleId Retrun.. NullReferenceException

        var roleInUsers = (from ru in usersInRoleRepository.UsersInRoles
                          where ru.RoleId == roleId
                          select ru.UserId).ToArray();

        var findUserResult = (from u in userRepository.Users
                              where roleInUsers.Contains(u.UserId) && u.UserAccount.Contains(userAccountToMatch)
                              select u.UserAccount).ToArray();

        return findUserResult;
    }

1 个答案:

答案 0 :(得分:0)

您的 cde 不是mackUserAccount 中的虚假用户。所以它返回null。

尝试以下代码字符串[] result = target.FindUsersInRole(“test”,“ abcdef ”);而不是

string[] result = target.FindUsersInRole("test", "cde");