0

I have a test method that is testing if users get added to a role. But my add users to that role doesnt work. Can someone explain what Im doing wrong?

 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        try
        {
            Profile.MembershipMapper memberMapper = new Profile.MembershipMapper();
            memberMapper.AddUsersToRoles(_memberUtil.GetApplicationId(), usernames, roleNames);
        }
        catch (Exception ex)
        {
            throw new ProviderException("AddUsersToRoles", ex);
        }
    }

            string sRoleName = "TestRole";
            string sUsername = "test.user";//"testUserX"

            // Use a known user for relationships
            AsaMembershipProvider prov = this.GetMembershipProvider();
            MembershipUser user = prov.GetUser(sUsername, false);

            // Create a new role
            AsaRoleProvider roleProv = this.GetRoleProvider();
            roleProv.CreateRole(sRoleName);

            // Verify that role exists
            bool bRoleExists = roleProv.RoleExists(sRoleName);
            Assert.IsTrue(bRoleExists);

            // Add users to that role
            string[] usernames = new string[] { sUsername};
            string[] roleNames = new string[] { sRoleName };

            //Doesnt add user
            roleProv.AddUsersToRoles(usernames, roleNames);
user216672
  • 151
  • 3
  • 5
  • 14
  • We need to see the code for the role provider if it's custom. Else we need to know which class you're using for that provider. – Geeky Guy May 23 '13 at 17:25
  • shouldn't compile ... `roleProv.AddUsersToRoles(usernames, roleNames);eNames);` – Alex May 23 '13 at 17:25

1 Answers1

1

Your code won't compile as is.

roleProv.AddUsersToRoles(usernames, roleNames);eNames); //syntax error probably

Assuming AddUsersToRoles has 3 parameters, try this:

roleProv.AddUsersToRoles(usernames, roleNames, eNames);

I don't see in your code where eNames is defined, but I'm also assuming you just have a basic syntax issue here.

Pay attention to your IDE. It's probably telling you what the issue is.

Additionally, if you just messed up copying your code over here, we have no way of telling why AddUsersToRoles isn't functioning properly as you haven't included that code.

In the future, include relevant code. Saying "it doesn't work" tells us nothing about your problem. Compile error? Runtime exception? Unexpected behavior?

tnw
  • 13,521
  • 15
  • 70
  • 111
  • Im having Unexpected behavior. As far as I can tell the test should pass and the user should be added to the role. When I debug and step into the code, I get Assert.Fail failed. AddUsersToRoles – user216672 May 23 '13 at 17:37
  • `Assert.Fail failed` indicates that there's an `Assert.Fail()` somewhere that intentionally errors out the method. – tnw May 23 '13 at 17:40
  • eNames, was an error in coping over. I have removed it and will post more code in a sec. – user216672 May 23 '13 at 17:40
  • @user216672 Something on this line is throwing the exception: `memberMapper.AddUsersToRoles(_memberUtil.GetApplicationId(), usernames, roleNames);` Track down the second `AddUsersToRoles` and `GetApplicationId` and find where that `Assert.Fail()` is. It's also possible that the constructor for `Profile.MembershipMapper` is throwing the `Assert.Fail` – tnw May 23 '13 at 17:45
  • Alternatively, if you're in Visual Studio, you can Ctrl+Shift+F and search Assert.Fail in your entire solution. – tnw May 23 '13 at 17:49
  • Thanks for the help. Im investigating memberMapper.AddUsersToRoles(_memberUtil.GetApplicationId(), usernames, roleNames); as suggested – user216672 May 23 '13 at 17:53
  • @user216672 Your comment is extremely confusing. Try one more time? – tnw May 23 '13 at 18:08