5

これがばかげた質問ではないことを願っています。私は自分のアクションで system.web.security.roles の呼び出しをモックする方法を 2 日間探していました。

単体テストを分離し、ロールが格納されるデータベースなどの外部リソースへの呼び出しを含めないようにしたいと考えています。

私が見つけたすべての回答は、カスタム ロール プロバイダーの作成を示唆していましたが、私は偽物の作成よりも嘲笑することを好みます。

私が得た最も近いのはこのソリューション でしたが、テスト外でプロジェクトを実行するときにninjectがデフォルトの役割プロバイダーをコンストラクターに渡す方法が見つかりませんでした。

タイプ Mock がこれに対するより簡単な解決策を提供することは知っていますが、私は rhino Mocks を使用しており、変更するには遅すぎます。

4

1 に答える 1

13

私はあなたと同じ問題を抱えており、2時間検索した後、あきらめました。

最後に、独自のモックロールプロバイダーを作成することになりました-コードベロー。

public class MockRoleProvider : RoleProvider
{
    public MockRoleProvider()
        : base()
    {
        Users = new List<User>();
        Roles = new List<Role>();
    }

    #region RoleProvider members
    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        if (usernames == null) throw new ArgumentNullException("usernames");
        if (roleNames == null) throw new ArgumentNullException("roleNames");

        foreach (string role in roleNames)
        {
            if (!RoleExists(role)) throw new ProviderException("Role name does not exist.");
        }

        foreach (string user in usernames)
        {
            if (Users.FirstOrDefault(u => u.Username == user) == null) throw new ProviderException("Username does not exist.");
        }

        foreach (string username in usernames)
        {
            User user = Users.FirstOrDefault(u => u.Username == username);
            if (user == null) continue;
            foreach (var rolename in roleNames)
            {
                Role role = Roles.FirstOrDefault(r => r.RoleName == rolename);
                user.Roles.Add(role);
                role.Users.Add(user);
            }
        }
    }

    public override string ApplicationName { get; set; }

    public override void CreateRole(string roleName)
    {
        if (roleName == null || roleName == "") throw new ProviderException("Role name cannot be empty or null.");
        if (roleName.Contains(",")) throw new ArgumentException("Role names cannot contain commas.");
        if (RoleExists(roleName)) throw new ProviderException("Role name already exists.");

        Roles.Add(new Role { RoleName = roleName });
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        if (roleName == null || roleName == "") throw new ProviderException("Role name cannot be empty or null.");

        Role role = Roles.FirstOrDefault(r => r.RoleName == roleName);

        if (role == null) throw new ProviderException("Role name does not exist.");
        if (throwOnPopulatedRole && GetUsersInRole(roleName).Length > 0) throw new ProviderException("Cannot delete a populated role.");

        Roles.Remove(Roles.FirstOrDefault(r => r.RoleName == roleName));
        return true;
    }

    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        return GetUsersInRole(roleName).Where(n => n.Contains(usernameToMatch)).ToArray();
    }

    public override string[] GetAllRoles()
    {
        return Roles.Select(r => r.RoleName).ToArray();
    }

    public override string[] GetRolesForUser(string username)
    {
        if (username == null || username == "") throw new ProviderException("User name cannot be empty or null.");

        User user = Users.FirstOrDefault(u => u.Username == username);

        if (user == null) return new string[0];

        return user.Roles.Select(r => r.RoleName).ToArray();
    }

    public override string[] GetUsersInRole(string roleName)
    {
        if (roleName == null || roleName == "") throw new ProviderException("Role name cannot be empty or null.");

        Role role = Roles.FirstOrDefault(r => r.RoleName == roleName);

        if (role == null) throw new ProviderException("Role '" + roleName + "' does not exist.");

        return role.Users.Select(u => u.Username).OrderBy(n => n).ToArray();
    }

    public override bool IsUserInRole(string username, string roleName)
    {
        if (username == null || username == "") throw new ProviderException("User name cannot be empty or null.");
        if (roleName == null || roleName == "") throw new ProviderException("Role name cannot be empty or null.");

        Role role = Roles.FirstOrDefault(r => r.RoleName == roleName);

        return role != null && role.Users.FirstOrDefault(u => u.Username == username) != null;
    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        foreach (string roleName in roleNames)
        {
            if (roleName == null || roleName == "") throw new ProviderException("Role name cannot be empty or null.");
            if (!RoleExists(roleName)) throw new ProviderException("Role name not found.");
        }

        foreach (string username in usernames)
        {
            if (username == null || username == "") throw new ProviderException("User name cannot be empty or null.");

            foreach (string roleName in roleNames)
            {
                if (!IsUserInRole(username, roleName)) throw new ProviderException("User is not in role.");
            }
        }

        foreach (string username in usernames)
        {
            User user = Users.FirstOrDefault(u => u.Username == username);
            if (user == null) continue;
            foreach (string roleName in roleNames)
            {
                Role role = user.Roles.FirstOrDefault(r => r.RoleName == roleName);
                role.Users.Remove(user);
                user.Roles.Remove(role);
            }
        }
    }

    public override bool RoleExists(string roleName)
    {
        if (roleName == null || roleName == "") throw new ProviderException("Role name cannot be empty or null.");

        return Roles.FirstOrDefault(r => r.RoleName == roleName) != null;
    }
    #endregion

    public void ClearAll()
    {
        Users = new List<User>();
        Roles = new List<Role>();
    }

    public void ClearRoles()
    {
        Roles = new List<Role>();
        Users.ForEach(u => u.Roles = new List<Role>());
    }

    public void ClearUsers()
    {
        Users = new List<User>();
        Roles.ForEach(r => r.Users = new List<User>());
    }

    public void CreateUser(string username)
    {
        if (username == null || username == "") throw new ProviderException("User name cannot be empty or null.");
        if (UserExists(username)) throw new ProviderException("User name already exists.");

        Users.Add(new User { Username = username });
    }

    public bool DeleteUser(string username)
    {
        if (username == null || username == "") throw new ProviderException("User name cannot be empty or null.");

        User user = Users.FirstOrDefault(u => u.Username == username);

        if (user == null) throw new ProviderException("User name does not exist.");

        foreach (Role role in user.Roles)
        {
            role.Users.Remove(user);
        }
        Users.Remove(user);
        return true;
    }

    public bool UserExists(string username)
    {
        if (username == null || username == "") throw new ProviderException("User name cannot be empty or null.");

        return Users.FirstOrDefault(u => u.Username == username) != null;
    }

    private List<Role> Roles { get; set; }

    private List<User> Users { get; set; }

    private class Role
    {
        public Role()
        {
            Users = new List<User>();
        }

        public string RoleName { get; set; }
        public List<User> Users { get; set; }
    }

    private class User
    {
        public User()
        {
            Roles = new List<Role>();
        }

        public string Username { get; set; }
        public List<Role> Roles { get; set; }
    }
}

これは本当に基本的ですが、RoleProvider の完全な実装です。

MockRoleProvider を使用するには、App.config に次の XML 行を<configuration>要素に追加します。

<system.web>
  <roleManager enabled="true" defaultProvider="MockRoleProvider">
    <providers>
      <add name="MockRoleProvider" type="YourProject.Tests.MockRoleProvider, YourProject.Tests"/>
    </providers>
  </roleManager>
</system.web>

次に、単体テストの配置部分で、次のコードを使用します。

MockRoleProvider mock = Roles.Provider as MockRoleProvider;
if (mock == null) throw new NullReferenceException("MockRoleProvider null exception.");
mock.ClearAll();
mock.CreateUser("SomeUser");
mock.CreateRole("SomeRole");
mock.AddUsersToRoles(new string[] { "SomeUser" }, new string[] { "SomeRole" });
于 2013-04-09T02:51:39.747 に答える