0

I am doing NUnit Testing for MVC controller's action Method ServiceConsumeToList() which uses OutputCache attribute. The method ServiceConsumeToList() exposes api service data. I want to test whether outputcache attribute caches the service data or not as follows. I applied for looping in nunit test method to test outputcaching action method ServiceConsumeToList() not hitting second time. But it hits ServiceConsumeToList() method 5 times as defined in loop of nunit test method. I am confused to test outputcache attribute applied mvc action method. Kindly put me in the right direction to test. Working suggestions would be greatly appreciated. Thanks :)

NUNIT TEST Class:

[TestCaseSource(typeof(mockData), nameof(mockData.calculation))]
        public void TestCaching(CalcRequest CalcRequest, CalcResponse CalcResponseExpect)
        {

        //Arrange
        _userService.CalcByService(CalcRequest).Returns(CalcResponseExpect);

            var mvcController1 = new mvcController1(_userService);

            IEnumerable<Mortgage> obj = null;

            for (int i = 0; i < 5; i++)
            {
                var result = mvcController1.ServiceConsumeToList() as JsonResult;
                obj = (IEnumerable<Mortgage>)result.Data;
            }

            Assert.IsNotNull(obj);
        }


//ASP.NET MVC CONTROLLER:   

[HttpGet]
        [OutputCache(Duration = 86400, VaryByParam = "none", Location = OutputCacheLocation.Server)]
        public JsonResult ServiceConsumeToList()
        {
            IEnumerable<CalcOuput> getServiceOuput = _Service.ServiceConsumeToList();
            return Json(getServiceOuput, JsonRequestBehavior.AllowGet);
        }
Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Brillian
  • 1,411
  • 2
  • 16
  • 23
  • 1
    `OutputCacheAttribute` is [tested in MVC](https://github.com/aspnet/AspNetWebStack/blob/15169a59e907be99d121c81fb646945c529f01a1/test/System.Web.WebPages.Test/Extensions/HttpResponseExtensionsTest.cs) already. You don't need to test its functionality, you need only check using Reflection whether it exists on your action method and whether the properties are correct. You might go the extra mile and test to ensure someone doesn't pull out the config bits from MVC that make it work if you feel it is worth the time. – NightOwl888 Feb 22 '18 at 15:34
  • my manager asked me to test the "outputcache" attribute caching the method output or not. He asked to write a unit test method to ensure caching happening at mvc controller. Kindly provide suggestions with example. and also provide idea about "someone doesn't pull out the config bits" as stated by you in the above comment. – Brillian Feb 23 '18 at 05:31
  • That is not a unit test, it is an integration test. Output caching is a feature of ASP.NET that MVC has exposes through this attribute. AFAIK, there is no way to make that feature work without actually *running* ASP.NET. See [this answer](https://stackoverflow.com/a/10379065) about how to self-host ASP.NET MVC to make it possible to use it in integration testing. Do note that means you need a computer with IIS 7+ installed in order to run your integration test. – NightOwl888 Feb 23 '18 at 15:01
  • As for the specifics on how to setup an integration test that runs ASP.NET MVC, that is another SO question topic that I don't have an answer for. I would recommend you start by doing a deep dive in the [MVC source code](https://github.com/aspnet/AspNetWebStack) first to see if there is any way to isolate a smaller portion of code so you don't have to setup such a complex test, and only ask how to setup ASP.NET MVC for integration testing the output cache if you cannot find a shortcut. – NightOwl888 Feb 23 '18 at 15:10
  • *also provide idea about "someone doesn't pull out the config bits"* - What I meant was to check [if the output cache is disabled or provider is wrong](https://weblogs.asp.net/scottgu/extensible-output-caching-with-asp-net-4-vs-2010-and-net-4-0-series) in the `web.config` and to research any other way (in both configuration and code) that this feature can be overridden or disabled to make sure nobody has tampered with the settings that make output caching function. – NightOwl888 Feb 23 '18 at 15:23

0 Answers0