lundi 9 mai 2016

Unit testing MVC app with Hangfire

Hi I am trying to setup unit testing for MVC 3 controllers that make use of Hangfire using OWin. Under normal operations, Hangfire gets configured in the Configuration function of the main controller like this

public void Configuration(IAppBuilder app)
{
    var sCon = ConnectionString.GetConnectionString();
    try
    {
        IUnitOfWork oUoW = UnitOfWorkFactory.GetInstance(sCon);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
        return;
    }
    app.UseHangfire(config =>
    {
        config.UseSqlServerStorage(ConnectionString.GetConnectionString());
        config.UseServer();
    });
}

I'm setting up HttpContextBase using Moq like this

private static HttpContextBase FakeAuthenticatedHttpContext()
{
    var context = new Mock<HttpContextBase>();
    var request = new Mock<HttpRequestBase>();
    var response = new Mock<HttpResponseBase>();
    var session = new Mock<HttpSessionStateBase>();
    var server = new Mock<HttpServerUtilityBase>();
    var user = new Mock<IPrincipal>();
    var identity = new Mock<IIdentity>();
    var application = new Mock<HttpApplicationStateBase>();

    context.Setup(ctx => ctx.Request).Returns(request.Object);
    context.Setup(ctx => ctx.Response).Returns(response.Object);
    context.Setup(ctx => ctx.Session).Returns(session.Object);
    context.Setup(ctx => ctx.Server).Returns(server.Object);
    context.Setup(ctx => ctx.User).Returns(user.Object);
    context.Setup(ctx => ctx.Application).Returns(application.Object);
    user.Setup(ctx => ctx.Identity).Returns(identity.Object);
    identity.Setup(id => id.IsAuthenticated).Returns(true);
    identity.Setup(id => id.Name).Returns("admin");

    return context.Object;
}

moBaseController.SetFakeAuthenticatedControllerContext();

How do I fake the call to Configuration to setup the Job Storage. I have been looking at the Hangfire documentation and they are a little cryptic in this area and mention that job storage should be set up something like this

GlobalConfiguration.Configuration.UseSqlServerStorage("<connection string or its name>");
using (new BackgroundJobServer())
{
   BackgroundJob.Enqueue(() => ProcessReport(oReportRequest, JobCancellationToken.Null)));

}

however GlobalConfiguration is not known anywhere in the controller or unit test.

Aucun commentaire:

Enregistrer un commentaire