public BackgroundJobStateChangerFacts()
        {
            _stateMachine   = new Mock <IStateMachine>();
            _filterProvider = new Mock <IJobFilterProvider>();
            _filterProvider.Setup(x => x.GetFilters(It.IsAny <Job>())).Returns(Enumerable.Empty <JobFilter>());

            _job   = Job.FromExpression(() => Console.WriteLine());
            _state = new Mock <IState>();
            _state.Setup(x => x.Name).Returns(StateName);

            _connection  = new Mock <IStorageConnection>();
            _transaction = new Mock <IWriteOnlyTransaction>();

            _connection.Setup(x => x.CreateWriteTransaction()).Returns(_transaction.Object);

            _connection.Setup(x => x.CreateExpiredJob(
                                  It.IsAny <Job>(),
                                  It.IsAny <IDictionary <string, string> >(),
                                  It.IsAny <DateTime>(),
                                  It.IsAny <TimeSpan>())).Returns(JobId);

            _connection.Setup(x => x.GetJobData(JobId))
            .Returns(new JobData
            {
                State = OldStateName,
                Job   = _job
            });

            _distributedLock = new Mock <IDisposable>();
            _connection
            .Setup(x => x.AcquireDistributedLock($"job:{JobId}:state-lock", It.IsAny <TimeSpan>()))
            .Returns(_distributedLock.Object);

            _cts     = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            _context = new StateChangeContextMock
            {
                BackgroundJobId   = JobId,
                Connection        = _connection,
                CancellationToken = _cts.Token,
                NewState          = _state,
                ExpectedStates    = FromOldState
            };

            _stateMachine.Setup(x => x.ApplyState(It.IsNotNull <ApplyStateContext>()))
            .Returns(_context.NewState.Object);
        }
        public BackgroundJobStateChangerFacts()
        {
            _stateMachine = new Mock<IStateMachine>();

            _job = Job.FromExpression(() => Console.WriteLine());
            _state = new Mock<IState>();
            _state.Setup(x => x.Name).Returns(StateName);
            
            _connection = new Mock<IStorageConnection>();
            _transaction = new Mock<IWriteOnlyTransaction>();

            _connection.Setup(x => x.CreateWriteTransaction()).Returns(_transaction.Object);

            _connection.Setup(x => x.CreateExpiredJob(
                It.IsAny<Job>(),
                It.IsAny<IDictionary<string, string>>(),
                It.IsAny<DateTime>(),
                It.IsAny<TimeSpan>())).Returns(JobId);

            _connection.Setup(x => x.GetJobData(JobId))
                .Returns(new JobData
                {
                    State = OldStateName,
                    Job = _job
                });

            _distributedLock = new Mock<IDisposable>();
            _connection
                .Setup(x => x.AcquireDistributedLock($"job:{JobId}:state-lock", It.IsAny<TimeSpan>()))
                .Returns(_distributedLock.Object);

            _cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            _context = new StateChangeContextMock
            {
                BackgroundJobId = JobId,
                Connection = _connection,
                CancellationToken = _cts.Token,
                NewState = _state,
                ExpectedStates = FromOldState
            };

            _stateMachine.Setup(x => x.ApplyState(It.IsNotNull<ApplyStateContext>()))
                .Returns(_context.NewState.Object);
        }