使用 Hangfire 对 MVC 应用程序进行单元测试
Unit testing MVC app with Hangfire
您好,我正在尝试使用 OWin 为使用 Hangfire 的 MVC 3 控制器设置单元测试。正常运行时,Hangfire在主控制器的Configuration函数中是这样配置的
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();
});
}
我正在像这样使用 Moq 设置 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();
如何伪造对配置的调用以设置作业存储?我一直在查看 Hangfire 文档,他们在这方面有点神秘,并提到作业存储应该像这样设置
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!");
一般在控制器中调用 Hangfire 时是这样的
oTokens.Add(oReportStatus.ID, BackgroundJob.Enqueue(() => ProcessReport(oReportRequest, JobCancellationToken.Null)));
根据@Rob 的建议,将此作为解决方案,我将所有 BackgroundJob.Enqueue() 引用更改为 IBackgroundJobClient bjc = GetJobClient(); bjc.Enqueue(...);并嘲笑 IBackgroundJobClient,并能够以这种方式进行测试。
假设您的代码类似于 RecurringJob.AddOrUpdate<MyJobClassType>(recurringJobId: "my recurring job id", methodCall: x => x.MyMethodAsync(id, null), cronExpression: "my cron");
,测试重复作业执行的一种方法是执行以下操作:
- 为您的单元测试设置内存存储 class(NuGet 包 Hangfire.MemoryStorage),因为我假设您不想将其保存到数据库。
- 执行安排重复作业的代码
- 从存储中获取重复作业并执行断言
using Hangfire;
using Hangfire.MemoryStorage;
using Hangfire.Storage;
[ClassInitialize]
public static void TestFixtureSetup(TestContext context)
{
var jobStorage = new MemoryStorage();
GlobalConfiguration.Configuration.UseStorage(jobStorage);
}
[TestMethod]
public void Test_RecurringJob_Scheduled_When_MyTestCondition()
{
// TODO: execute code that schedules recurring jobs
using (var connection = JobStorage.Current.GetConnection())
{
var recurringJobs = connection.GetRecurringJobs();
// TODO: asserts
}
}
您好,我正在尝试使用 OWin 为使用 Hangfire 的 MVC 3 控制器设置单元测试。正常运行时,Hangfire在主控制器的Configuration函数中是这样配置的
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();
});
}
我正在像这样使用 Moq 设置 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();
如何伪造对配置的调用以设置作业存储?我一直在查看 Hangfire 文档,他们在这方面有点神秘,并提到作业存储应该像这样设置
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!");
一般在控制器中调用 Hangfire 时是这样的
oTokens.Add(oReportStatus.ID, BackgroundJob.Enqueue(() => ProcessReport(oReportRequest, JobCancellationToken.Null)));
根据@Rob 的建议,将此作为解决方案,我将所有 BackgroundJob.Enqueue() 引用更改为 IBackgroundJobClient bjc = GetJobClient(); bjc.Enqueue(...);并嘲笑 IBackgroundJobClient,并能够以这种方式进行测试。
假设您的代码类似于 RecurringJob.AddOrUpdate<MyJobClassType>(recurringJobId: "my recurring job id", methodCall: x => x.MyMethodAsync(id, null), cronExpression: "my cron");
,测试重复作业执行的一种方法是执行以下操作:
- 为您的单元测试设置内存存储 class(NuGet 包 Hangfire.MemoryStorage),因为我假设您不想将其保存到数据库。
- 执行安排重复作业的代码
- 从存储中获取重复作业并执行断言
using Hangfire;
using Hangfire.MemoryStorage;
using Hangfire.Storage;
[ClassInitialize]
public static void TestFixtureSetup(TestContext context)
{
var jobStorage = new MemoryStorage();
GlobalConfiguration.Configuration.UseStorage(jobStorage);
}
[TestMethod]
public void Test_RecurringJob_Scheduled_When_MyTestCondition()
{
// TODO: execute code that schedules recurring jobs
using (var connection = JobStorage.Current.GetConnection())
{
var recurringJobs = connection.GetRecurringJobs();
// TODO: asserts
}
}