1

Here in the controller

var roles = Roles.GetAllRoles(); 

fails from the Unit test call and in the

LogOnTest()(Error message is "The Role Manager feature has not been enabled.")

Could you please tell me whether my test function is correct or need any more logical/functional test?

[TestMethod]
public void LogOnTest()
{
    var target = new AccountController();
    var membershipMock = MockRepository.GenerateMock<AccountMembershipService>();
    var formsMock = MockRepository.GenerateMock<IFormsAuthenticationService>();

    target.FormsService = formsMock;
    target.MembershipService = membershipMock;

    var model = new LogonModel() { USERNAME= "aa", Password = "aa"};
    string returnUrl = null;
    bool isLoginSuccess = true;
    var actual = target.LogOnFromUser(model);
    if (actual == null)
        Assert.Fail("should have redirected");
 }

Controller:

public ActionResult LogOn(LogonModel model)
        {
if(MembershipService.ValidateUser(model.UsernName, model.Password))
                {
                    FormsService.SignIn(model.UsernName, true);
                                        var roles = Roles.GetAllRoles(); //Roles got failed here
return RedirectToAction("Index", "Event");
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");

            return View("LogOn", model);
}
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
Mohan
  • 11
  • 3
  • if you find an answer useful, you should vote for it (click the up arrow at the top left of the answer) and if it answers your question you should also accept it as an answer by clicking the tick mark also at the top left of the answer – Sam Holder Oct 04 '11 at 20:18

2 Answers2

1

You need to create a wrapper around the static Roles class so that you can inject a mock implementation which you are in control of. Basically this will probably be similar to your MembershipService and FormsService (probably call it IRoleService) and will need to have the method GetAllRoles().

You could create the default implementation (which would just delegate to the current static class) in the null arg constructor for your controller (which is what I imagine happens with your membership and forms services, and provide a property to allow you to override it in the smae way as the others.

Another option would be to extend your membership service to allow you to call this method, rather than creating another service specifically for roles. This is against the Single Responsibility Principle, so not necessarily a great idea, but it does mean you don't have to inject another dependency, and it could be argued that getting the roles of a member is within the remit of the membership service.

To be honest I think you would be better changing your control to have a constructor which explicitly asks for implementations of the membership service, forms service and roles service and providing mocks in the tests. This makes it explicit what the dependencies of the controller are. You would need to then step up to a new way of creating the controller as the default can only create them if they have default constructors. YUou have a couple of choices here. Either keep the option you have and use defaults, or implement your own ControllerFactory. Some details here. I prefer the ControllerFactory approach personally.

Community
  • 1
  • 1
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
1

In theory, I prefer @Sam's approach, but I am still working on getting it to fit in with my dependency injection. I really want to be able to mock up my own data and get full control over my tests.

As a stop gap, however, I found a suggestion here: http://blog.gfader.com/2009/11/aspnet-how-to-show-all-roles-of-current.html

From this I just added the code below to the App.config and I could move along with testing.

 <system.web>
      <roleManager enabled="true"
             defaultProvider="AspNetWindowsTokenRoleProvider" />
    </system.web>
wilsjd
  • 2,178
  • 2
  • 23
  • 37