Skip to content

Instantly share code, notes, and snippets.

@odinserj
Created November 29, 2018 13:03
Show Gist options
  • Save odinserj/4046899bcc9ef6cff63bb614358594e6 to your computer and use it in GitHub Desktop.
Save odinserj/4046899bcc9ef6cff63bb614358594e6 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using Hangfire;
using Hangfire.Batches.Client;
using Hangfire.Client;
using Hangfire.Storage;
using Moq;
using Xunit;
namespace ClassLibrary1
{
public class BatchClientFacts
{
private readonly Mock<JobStorage> _storage = new Mock<JobStorage>();
private readonly Mock<JobStorageConnection> _connection = new Mock<JobStorageConnection>();
private readonly Mock<JobStorageTransaction> _transaction = new Mock<JobStorageTransaction>();
private readonly Mock<IBatchFactory> _batchFactory = new Mock<IBatchFactory>();
private readonly Mock<IBackgroundJobFactory> _jobFactory = new Mock<IBackgroundJobFactory>();
private readonly BatchJobClient _batchClient;
public BatchClientFacts()
{
GlobalConfiguration.Configuration.UseBatches();
_storage.Setup(x => x.GetConnection()).Returns(_connection.Object);
_connection.Setup(x => x.CreateWriteTransaction()).Returns(_transaction.Object);
_jobFactory
.Setup(x => x.Create(It.IsAny<CreateContext>()))
.Returns((CreateContext context) => new BackgroundJob("MyJob", context.Job, DateTime.UtcNow));
_batchClient = new BatchJobClient(_storage.Object, _batchFactory.Object, _jobFactory.Object);
}
[Fact]
public void StartNew_CanCreateABatchWithASingleBackgroundJob()
{
_batchClient.StartNew(batch =>
{
batch.Enqueue(() => Console.WriteLine("Hello!"));
});
_batchFactory.Verify(x => x.Create(It.Is<BatchCreateContext>(context =>
context.InitialState.Name == "Started" &&
context.BatchedJobs.SingleOrDefault().BackgroundJob.Job.Method.Name == "WriteLine" &&
context.BatchedJobs.SingleOrDefault().BackgroundJob.Job.Args[0].ToString() == "Hello!")));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment