Aha. Gotcha. Fixed most of the failing tests now - thanks for pointing that out to me. I'm struggling still with some tests in which I'm creating Users and adding them to my membership store. Here's the test code:
private static MembershipUserCollection CreateDefaultUsers() {
MembershipUserCollection users = new MembershipUserCollection();
users.Add(new MembershipUser("AspNetSqlMembershipProvider", "simon.rigby", new Guid("05b75472-2a41-4cef-84ce-cea5517a80b9"), "simon@yourgolftravel.com",
"question", null, true, false, new DateTime(2007, 10, 31), new DateTime(2007, 10, 31), new DateTime(2007, 10, 31),
new DateTime(2007, 10, 31), new DateTime(2007, 10, 31)));
return users;
}
[Test]
public void AddUser() {
MembershipUserCollection defaultUsers = CreateDefaultUsers();
MembershipUserCollection users = new MembershipUserCollection();
MembershipUser user = new MembershipUser("AspNetSqlMembershipProvider", "bob.smith", new Guid("abd70434-bb5d-4905-86a2-19d9ac769086"), "bob@yourgolftravel.com",
"question", null, true, false, new DateTime(2007, 10, 31), new DateTime(2007, 10, 31), new DateTime(2007, 10, 31),
new DateTime(2007, 10, 31), new DateTime(2007, 10, 31));
users.Add(user);
users.Add(defaultUsers["simon.rigby"]);
string defaultPassword = "password";
TextBox tbUserName = new TextBox();
TextBox tbEmail = new TextBox();
CheckBoxList checkBoxList = CreateRoleCheckBoxList();
checkBoxList.Items[1].Selected = true;
checkBoxList.Items[2].Selected = true;
string[] roles = new string[] {"SalesAdministrator", "Salesperson"};
Label lblAddUser = new Label();
tbUserName.Text = "bob.smith";
tbEmail.Text = "bob@yourgolftravel.com";
Table tblUsers = CreateUsersTable(false);
Mock extensionMock = MockManager.Mock<AdminUserFormTestExtension>();
Mock membershipMock = MockManager.Mock(typeof (Membership));
Mock webConfigurationManagerMock = MockManager.Mock(typeof (WebConfigurationManager));
MockObject appSettingsMockObject = MockManager.MockObject<NameValueCollection>();
Mock rolesMock = MockManager.MockAll(typeof (Roles));
rolesMock.AlwaysReturn("GetAllRoles", _roles.ToArray());
extensionMock.ExpectAndReturn("FindControl", tbUserName).Args("tbUserName_AdminUserForm");
extensionMock.ExpectAndReturn("FindControl", tbEmail).Args("tbEmail_AdminUserForm");
extensionMock.ExpectAndReturn("FindControl", checkBoxList).Args("cblUserRoles0_AdminUserForm");
extensionMock.ExpectAndReturn("FindControl", lblAddUser).Args("lblAddUser_AdminUserForm");
extensionMock.ExpectAndReturn("FindControl", tblUsers).Args("tblUsers_AdminUserForm");
webConfigurationManagerMock.ExpectGet("AppSettings", appSettingsMockObject.Object);
appSettingsMockObject.ExpectAndReturn("Get", defaultPassword).Args("defaultPassword");
membershipMock.ExpectAndReturn("CreateUser", user).Args("bob.smith", defaultPassword, "bob@yourgolftravel.com");
rolesMock.ExpectCall("AddUserToRoles").Args("bob.smith", roles);
rolesMock.ExpectAndReturn("IsUserInRole", false).Args("bob.smith", _roles[0]);
rolesMock.ExpectAndReturn("IsUserInRole", true).Args("bob.smith", _roles[1]);
rolesMock.ExpectAndReturn("IsUserInRole", true).Args("bob.smith", _roles[2]);
membershipMock.ExpectAndReturn("GetAllUsers", users);
AdminUserFormTestExtension extension = new AdminUserFormTestExtension();
extension.AddUser(new Button(), EventArgs.Empty);
Assert.AreEqual("ok", lblAddUser.CssClass);
Assert.AreEqual("User bob.smith was created ok", lblAddUser.Text);
Assert.AreEqual(6, tblUsers.Rows.Count);
Assert.AreEqual("", tblUsers.Rows[1].CssClass);
Assert.AreEqual("alternate", tblUsers.Rows[2].CssClass);
Assert.AreEqual("", tblUsers.Rows[3].CssClass);
Assert.AreEqual("alternate", tblUsers.Rows[4].CssClass);
Assert.AreEqual("", tblUsers.Rows[5].CssClass);
Assert.AreEqual("bob.smith", tblUsers.Rows[2].Cells[1].Text);
}
The error starts in the MembershipUser constructor within CreateDefaultUsers, but I'm a bit stuck after that. Any ideas? I've tried mocking the MembershipUser, but that just stops the actual MembershipU