2016-05-09 66 views
2

嗨我正在嘗試爲使用OWin使用Hangfire的MVC 3控制器設置單元測試。在正常操作下,遲髮型被主控制器這樣使用Hangfire單元測試MVC應用程序

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

配置功能配置我設立HttpContextBase使用起訂量這樣

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

我如何假調用配置,以設置作業存儲?我一直在尋找的遲髮型文件,他們是有點神祕在這一領域,並註明作業存儲應該建立這樣

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

} 

然而GlobalConfiguration未在控制器或單元測試的任何地方知道的東西。

我使用的是Hangfire版本1.1.1和OWin 2.1。總之,我正在尋找一種方法來模擬MVC 3方法調用的環境,以透明地設置作業存儲和後臺服務器。即我如何模擬控制器,以便在調用該方法時已設置好。

這通常是我如何調用在單元測試控制器:

oViewResult = (ViewResult)moController.RunExport(oRequest); 

Assert.IsNotNull(oViewResult, "Didn't return a view result!"); 
Assert.IsTrue(oViewResult.ViewName == "RunReport", "Didn't return a valid view name!"); 
Assert.IsTrue(oViewResult.Model != null, "No Model response!"); 

var oResult = (string)oViewResult.Model; 
Assert.IsTrue(oResult == "Ok", "Export did not run as expected!"); 

一般當遲髮型被稱爲是在控制器

oTokens.Add(oReportStatus.ID, BackgroundJob.Enqueue(() => ProcessReport(oReportRequest, JobCancellationToken.Null))); 
+0

幾件事情。首先,'GlobalConfiguration'只是一個有很多靜態方法的類,我想你可以在'Hangfire'命名空間中找到它。其次,爲什麼你需要測試任何與Hangfire有關的事情?當然,你只需要測試你的工作方法。 – DavidG

+0

我不需要測試Hangfire,我需要測試控制器發生調用Hangfire的方法,即我需要模擬Hangfire環境,作業存儲,BackgroundServer等。 – aggaton

+1

您是否[閱讀](http:/ /docs.hangfire.io/en/latest/background-methods/writing-unit-tests.html)?而不是使用'BackgroundJob.Enqueue()',你應該從你選擇的對象工廠/ DI中抓取'IBackgroundJobClient',而不是使用靜態類。也就是說,你會運行'IBackgroundJobClient bjc = GetJobClient(); bjc.Enqueue(...);'然後你簡單地模擬'IBackgroundJobClient'。 – Rob

回答

0

配售以此作爲解決這樣做,根據@ Rob的建議,我將所有的BackgroundJob.Enqueue()引用改爲IBackgroundJobClient bjc = GetJobClient(); bjc.Enqueue(...);並嘲笑IBackgroundJobClient,並能夠以這種方式進行測試。

+0

但您不能使用帶有'IBackgroundJobClient'的計劃重複作業.. – Esen