문제

I'm doing unit testing for my controller's method. In following method, I want to check if the return type is ViewResult

    [HttpGet]
    public ActionResult AddRepresentative(Guid businessUnitId)
    {
        var roles = Roles.GetAllRoles();
        var model = new AddRepresentativeModel
        {
            BusinessUnitId = businessUnitId,
            Roles = roles.Select(r => new SelectListItem
            {
                Value = r,
                Text = r,
            })
        };
        return View(model);
    }

This is what i've done for testing

   [TestMethod]
    public void AddRepresentative_Get_Action_RendersView()
    {
        var result = _controller.AddRepresentative(Guid.Empty);

        Assert.IsInstanceOfType(result,typeof(ViewResult));
    }

Error:

Test method AdminPortal.Tests.Controller_Test.Customer.BusinessUnitControllerTests.AddRepresentative_Get_Action_RendersView threw exception: System.Configuration.Provider.ProviderException: The Role Manager feature has not been enabled.

Question: Is it not good practice to collect list of roles in HttpGet method?

Is there some change that i need to make in my controller or test method in order make the test pass?

Is the error occurring because I'm using RhinoMock?

도움이 되었습니까?

해결책

Is it not good practice to collect list of roles in HttpGet method?

There's nothing wrong with it.

Is there some change that i need to make in my controller or test method in order make the test pass?

Yes, you could add an app.config file to your unit test project and register a role manager in the <system.web> section, the same way you did in your actual application. Otherwise where do you think the Roles.GetAllRoles(); method will get any data from? Alternatively you could wrap this call in a service layer that could be mocked in your unit test.

Is the error occurring because I'm using RhinoMock?

No.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top