unit-testingumbracoumbraco7argumentnullexception

How to unit test Umbraco Api controller - System.ArgumentNullException: 'Value cannot be null. Parameter name: umbracoContext'


I'm trying to write unit tests for an UmbracoApiController but when running the tests I'm getting an error when creating a new instance of the api controller.

I've started to look into mocking the umbraco context but haven't made much progress as I'm so new to it.

[TestMethod]
public void GetAllMembers()
{
    var c = new GroupManagerApiController();
    Assert.Fail();
}

Controller

public class GroupManagerApiController : UmbracoApiController

When creating a new instance of the controller I'm getting the following error message:

System.ArgumentNullException: 'Value cannot be null.
Parameter name: umbracoContext'

Kindly guide me for this.

Thank You.


Solution

  • http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco/

    This blog solved the issue.

    Need to create a setup method with the following:

    [SetUp]
    public void SetUp()
    {
        var applicationContext = new ApplicationContext(
            CacheHelper.CreateDisabledCacheHelper(),
            new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>())
        );
        UmbracoContext.EnsureContext(
            Mock.Of<HttpContextBase>(),
            applicationContext,
            new WebSecurity(Mock.Of<HttpContextBase>(), applicationContext),
            Mock.Of<IUmbracoSettingsSection>(),
            Enumerable.Empty<IUrlProvider>(),
            true
        );
    }