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);
}