I am trying to unit test this application that uses application variables. There is no interfaces or virtual methods in the program and I am finding it difficult to make Moq work.
Class that uses Application Variables:
static string Username = HttpContext.Current.Application["Username"].ToString();
It is initialized in the global.asax
file.
My Unit test:
[TestMethod]
public void TestGetCompanyList()
{
Mock<HttpContextBase> context = new Mock<HttpContextBase>();
Mock<HttpApplication> app = new Mock<HttpApplication>();
context.Setup(ctx => ctx.ApplicationInstance).Returns(app); //ERROR
var accountController = new AccountServiceController();
accountController.ControllerContext = new ControllerContext(context.Object, new RouteData(), accountController); //ERROR
CompInput cInput = new CompInput();
cInput.IssuerName = "Addams";
cInput.Ticker = "AD";
var result = accountController.CompList(cInput) as IEnumerable<CompListResult>;
Assert.IsNotNull(result);
}
The first error:
Error 4 The best overloaded method match for Moq.Language.IReturns<System.Web.HttpContextBase,System.Web.HttpApplication>.Returns(System.Web.HttpApplication)' has some invalid arguments
Error 5 Argument 1: cannot convert from 'Moq.Mock<System.Web.HttpApplication>' to 'System.Web.HttpApplication'
Second Error:
Error 6 The best overloaded method match for 'System.Web.Mvc.ControllerContext.ControllerContext(System.Web.HttpContextBase, System.Web.Routing.RouteData, System.Web.Mvc.ControllerBase)' has some invalid arguments
Error 7 Argument 3: cannot convert from 'Stocktrage.Investor.AccountServiceAPI.Controllers.AccountServiceController' to 'System.Web.Mvc.ControllerBase'
This is my first time using Moq (or any mocking tool), so I am not 100% sure if this can be done without interfaces.