public void Current_WithOnlyDefault_ShouldSucceedForUnitTestButRequireRegistrationOtherwise() { using (new TestDetector(isTestRun: false)) { if (TestDetector.IsTestRun) { throw new Exception("Test detector wrongfully detected test run."); } Assert.Throws <InvalidOperationException>(() => IdGeneratorScope.Current); var hostBuilder = new HostBuilder(); hostBuilder.ConfigureServices(services => { services .AddApplicationInstanceIdSource(source => source.UseFixedSource(1)) .AddIdGenerator(generator => generator.UseFluid()); }); using (var host = hostBuilder.Build()) { Assert.Throws <InvalidOperationException>(() => IdGeneratorScope.Current); host.UseIdGenerator(); Assert.NotNull(IdGeneratorScope.Current); } IdGeneratorScope.SetDefaultValue(null); Assert.Throws <InvalidOperationException>(() => IdGeneratorScope.Current); } if (!TestDetector.IsTestRun) { throw new Exception("Test detector failed to detect test run."); } Assert.NotNull(IdGeneratorScope.Current); }
public void Current_WithLocalScope_ShouldUseScopedGenerator() { var invocationDetector = new InvocationDetector(); using var scope = new IdGeneratorScope(CreateIdGenerator(invocationDetector)); Assert.True(invocationDetector.WasInvoked); }
public void CurrentGenerator_WithLocalScopeWhenUsed_ShouldUseScopedGenerator() { var invocationDetector = new InvocationDetector(); using var scope = new IdGeneratorScope(CreateIdGenerator(invocationDetector)); invocationDetector.WasInvoked = false; IdGeneratorScope.CurrentGenerator.CreateId(); Assert.True(invocationDetector.WasInvoked); }
public void Construct_Regularly_ShouldReturnExpectedResult() { using var idScope = new IdGeneratorScope(new CustomIdGenerator(id: 1)); using var clockScope = new ClockScope(() => DateTime.UnixEpoch); var orderBuilder = new OrderBuilder() .WithDescription("Description"); var order = orderBuilder.Build(); Assert.Equal(1, order.Id); Assert.Equal(DateTime.UnixEpoch, order.CreationDateTime); Assert.Equal(order.CreationDateTime, order.ShippingStatus.DateTime); Assert.Equal(ShippingResult.Unshipped, order.ShippingStatus.Result); Assert.Equal("Description", order.Description); }
public void CurrentGenerator_WithNestedScopesWhenUsed_ShouldUseNearestScopedGenerator() { var outerInvocationDetector = new InvocationDetector(); using var outerScope = new IdGeneratorScope(CreateIdGenerator(outerInvocationDetector)); outerInvocationDetector.WasInvoked = false; var innerInvocationDetector = new InvocationDetector(); using var innerScope = new IdGeneratorScope(CreateIdGenerator(innerInvocationDetector)); innerInvocationDetector.WasInvoked = false; IdGeneratorScope.CurrentGenerator.CreateId(); Assert.True(innerInvocationDetector.WasInvoked); Assert.False(outerInvocationDetector.WasInvoked); }