Skip to content

Commit

Permalink
Adding test for IJobs with decorators (increasing coverage)
Browse files Browse the repository at this point in the history
  • Loading branch information
tynor88 committed Dec 26, 2015
1 parent 780e9d1 commit 76f8583
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ private static void Main(string[] args)
{
//Simple Repeatable Scheduled from TimeSpan
s.ScheduleQuartzJob(configurator =>
configurator.WithSimpleRepeatableSchedule<JobWithInjectedDependenciesDecorated>(
TimeSpan.FromSeconds(1), "JobWithInjectedDependenciesDecorated"));
configurator.WithSimpleRepeatableSchedule<JobWithInjectedDependenciesDecorated>(TimeSpan.FromSeconds(1), nameof(JobWithInjectedDependenciesDecorated)));
// Let Topshelf use it
s.ConstructUsingSimpleInjector();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;
using Moq;
using NUnit.Framework;
using Quartz;
Expand Down Expand Up @@ -27,22 +28,20 @@ public void QuartzJobIsExecutedSuccessfullyTest()
_container.Register<ISampleDependency, SampleDependency>();

//Act
var host =
HostFactory.New(config =>
var host = HostFactory.New(config =>
{
config.UseTestHost();
config.UseQuartzSimpleInjector(_container);
_container.Verify();
config.Service<TestService>(s =>
{
config.UseTestHost();
config.UseQuartzSimpleInjector(_container);
_container.Verify();
config.Service<TestService>(s =>
{
s.ScheduleQuartzJob(
configurator => configurator.WithSimpleRepeatableSchedule<IJob>(TimeSpan.FromMilliseconds(1)));
s.ConstructUsingSimpleInjector();
s.WhenStarted((service, control) => service.Start());
s.WhenStopped((service, control) => service.Stop());
});
s.ScheduleQuartzJob(configurator => configurator.WithSimpleRepeatableSchedule<IJob>(TimeSpan.FromMilliseconds(1)));
s.ConstructUsingSimpleInjector();
s.WhenStarted((service, control) => service.Start());
s.WhenStopped((service, control) => service.Stop());
});
});

var exitCode = host.Run();

Expand All @@ -61,24 +60,21 @@ public void JobFactoryIsCorrectlyUsedForIJobCreationTest()
Mock<IJobFactory> factoryMock = new Mock<IJobFactory>();

//Act
var host =
HostFactory.New(config =>
var host = HostFactory.New(config =>
{
config.UsingQuartzJobFactory<IJobFactory>(() => factoryMock.Object);
config.UseTestHost();
config.UseQuartzSimpleInjector(_container);
_container.Verify();
config.Service<TestService>(s =>
{
config.UsingQuartzJobFactory<IJobFactory>(() => factoryMock.Object);
config.UseTestHost();
config.UseQuartzSimpleInjector(_container);
_container.Verify();
config.Service<TestService>(s =>
{
s.ScheduleQuartzJob(
configurator =>
configurator.WithSimpleRepeatableSchedule<IJob>(TimeSpan.FromMilliseconds(1)));
s.ConstructUsingSimpleInjector();
s.WhenStarted((service, control) => service.Start());
s.WhenStopped((service, control) => service.Stop());
});
s.ScheduleQuartzJob(configurator => configurator.WithSimpleRepeatableSchedule<IJob>(TimeSpan.FromMilliseconds(1)));
s.ConstructUsingSimpleInjector();
s.WhenStarted((service, control) => service.Start());
s.WhenStopped((service, control) => service.Stop());
});
});

var exitCode = host.Run();

Expand All @@ -104,6 +100,39 @@ public void ExceptionIsThrownWhenTheContainerIsNullTest()
//Assert
Assert.AreEqual("Value cannot be null.\r\nParameter name: container", exception.Message);
}

[Test, RunInApplicationDomain]
public void DecoratedJobsAreCorrectlyExecutingTest()
{
//Arrange
Mock<IDecoratorDependency> decoratorDependencyMock = new Mock<IDecoratorDependency>();

_container.RegisterDecorator<IJob, TestJobDecorator>();
_container.Register<IDecoratorDependency>(() => decoratorDependencyMock.Object);
_container.Register<ISampleDependency, SampleDependency>();

//Act
var host = HostFactory.New(config =>
{
config.UseQuartzSimpleInjectorWithDecorators(_container, Assembly.GetExecutingAssembly());
config.UseTestHost();
_container.Verify();
config.Service<TestService>(s =>
{
s.ScheduleQuartzJob(configurator => configurator.WithSimpleRepeatableSchedule<TestJob>(TimeSpan.FromMilliseconds(1), nameof(TestJob)));
s.ConstructUsingSimpleInjector();
s.WhenStarted((service, control) => service.Start());
s.WhenStopped((service, control) => service.Stop());
});
});

var exitCode = host.Run();

//Assert
Assert.AreEqual(TopshelfExitCode.Ok, exitCode);
decoratorDependencyMock.Verify(dependency => dependency.DoSomething(), Times.AtLeastOnce);
}
}

public class TestService
Expand Down Expand Up @@ -144,4 +173,27 @@ public void Execute(IJobExecutionContext context)
Console.WriteLine("Executing...");
}
}

public interface IDecoratorDependency
{
void DoSomething();
}

public class TestJobDecorator : IJob
{
private readonly IJob _jobDecoratee;
private readonly IDecoratorDependency _decoratorDependency;

public TestJobDecorator(IJob jobDecoratee, IDecoratorDependency decoratorDependency)
{
_jobDecoratee = jobDecoratee;
_decoratorDependency = decoratorDependency;
}

public void Execute(IJobExecutionContext context)
{
_jobDecoratee.Execute(context);
_decoratorDependency.DoSomething();
}
}
}

0 comments on commit 76f8583

Please sign in to comment.