Skip to content

Commit 2697b4a

Browse files
author
Bart Koelman
committed
Updated tests no never use the real clock
1 parent ff9c287 commit 2697b4a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+221
-227
lines changed

src/Examples/JsonApiDotNetCoreExample/Startups/KebabCaseStartup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace JsonApiDotNetCoreExample
88
/// This should be in JsonApiDotNetCoreExampleTests project but changes in .net core 3.0
99
/// do no longer allow that. See https://github.com/aspnet/AspNetCore/issues/15373.
1010
/// </summary>
11-
public sealed class KebabCaseStartup : Startup
11+
public sealed class KebabCaseStartup : TestStartup
1212
{
1313
public KebabCaseStartup(IWebHostEnvironment env) : base(env)
1414
{

src/Examples/JsonApiDotNetCoreExample/Startups/MetaStartup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace JsonApiDotNetCoreExample
99
/// This should be in JsonApiDotNetCoreExampleTests project but changes in .net core 3.0
1010
/// do no longer allow that. See https://github.com/aspnet/AspNetCore/issues/15373.
1111
/// </summary>
12-
public sealed class MetaStartup : Startup
12+
public sealed class MetaStartup : TestStartup
1313
{
1414
public MetaStartup(IWebHostEnvironment env) : base(env) { }
1515

src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace JsonApiDotNetCoreExample
77
/// This should be in JsonApiDotNetCoreExampleTests project but changes in .net core 3.0
88
/// do no longer allow that. See https://github.com/aspnet/AspNetCore/issues/15373.
99
/// </summary>
10-
public sealed class NoDefaultPageSizeStartup : Startup
10+
public sealed class NoDefaultPageSizeStartup : TestStartup
1111
{
1212
public NoDefaultPageSizeStartup(IWebHostEnvironment env) : base(env) { }
1313

src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public Startup(IWebHostEnvironment env)
3030

3131
public virtual void ConfigureServices(IServiceCollection services)
3232
{
33-
services.AddSingleton<ISystemClock, SystemClock>();
33+
ConfigureClock(services);
34+
3435
services.AddScoped<SkipCacheQueryParameterService>();
3536
services.AddScoped<IQueryParameterService>(sp => sp.GetService<SkipCacheQueryParameterService>());
3637

@@ -47,6 +48,11 @@ public virtual void ConfigureServices(IServiceCollection services)
4748
services.AddClientSerialization();
4849
}
4950

51+
protected virtual void ConfigureClock(IServiceCollection services)
52+
{
53+
services.AddSingleton<ISystemClock, SystemClock>();
54+
}
55+
5056
protected virtual void ConfigureJsonApiOptions(JsonApiOptions options)
5157
{
5258
options.IncludeExceptionStackTraceInErrors = true;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace JsonApiDotNetCoreExample
7+
{
8+
public class TestStartup : Startup
9+
{
10+
public TestStartup(IWebHostEnvironment env) : base(env)
11+
{
12+
}
13+
14+
protected override void ConfigureClock(IServiceCollection services)
15+
{
16+
services.AddSingleton<ISystemClock, AlwaysChangingSystemClock>();
17+
}
18+
19+
private class AlwaysChangingSystemClock : ISystemClock
20+
{
21+
private DateTimeOffset _utcNow;
22+
23+
public DateTimeOffset UtcNow
24+
{
25+
get
26+
{
27+
var utcNow = _utcNow;
28+
_utcNow = _utcNow.AddSeconds(1);
29+
return utcNow;
30+
}
31+
}
32+
33+
public AlwaysChangingSystemClock()
34+
: this(new DateTimeOffset(new DateTime(2000, 1, 1)))
35+
{
36+
}
37+
38+
public AlwaysChangingSystemClock(DateTimeOffset utcNow)
39+
{
40+
_utcNow = utcNow;
41+
}
42+
}
43+
}
44+
}

test/JsonApiDotNetCoreExampleTests/Acceptance/ActionResultTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace JsonApiDotNetCoreExampleTests.Acceptance
1414
[Collection("WebHostCollection")]
1515
public sealed class ActionResultTests
1616
{
17-
private readonly TestFixture<Startup> _fixture;
17+
private readonly TestFixture<TestStartup> _fixture;
1818

19-
public ActionResultTests(TestFixture<Startup> fixture)
19+
public ActionResultTests(TestFixture<TestStartup> fixture)
2020
{
2121
_fixture = fixture;
2222
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/CustomControllerTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility
2121
[Collection("WebHostCollection")]
2222
public sealed class CustomControllerTests
2323
{
24-
private readonly TestFixture<Startup> _fixture;
24+
private readonly TestFixture<TestStartup> _fixture;
2525
private readonly Faker<TodoItem> _todoItemFaker;
2626
private readonly Faker<Person> _personFaker;
2727

28-
public CustomControllerTests(TestFixture<Startup> fixture)
28+
public CustomControllerTests(TestFixture<TestStartup> fixture)
2929
{
3030
_fixture = fixture;
3131
_todoItemFaker = new Faker<TodoItem>()
@@ -41,7 +41,7 @@ public async Task NonJsonApiControllers_DoNotUse_Dasherized_Routes()
4141
{
4242
// Arrange
4343
var builder = new WebHostBuilder()
44-
.UseStartup<Startup>();
44+
.UseStartup<TestStartup>();
4545
var httpMethod = new HttpMethod("GET");
4646
var route = "testValues";
4747

@@ -61,7 +61,7 @@ public async Task CustomRouteControllers_Uses_Dasherized_Collection_Route()
6161
{
6262
// Arrange
6363
var builder = new WebHostBuilder()
64-
.UseStartup<Startup>();
64+
.UseStartup<TestStartup>();
6565
var httpMethod = new HttpMethod("GET");
6666
var route = "/custom/route/todoItems";
6767

@@ -88,7 +88,7 @@ public async Task CustomRouteControllers_Uses_Dasherized_Item_Route()
8888
await context.SaveChangesAsync();
8989

9090
var builder = new WebHostBuilder()
91-
.UseStartup<Startup>();
91+
.UseStartup<TestStartup>();
9292
var httpMethod = new HttpMethod("GET");
9393
var route = $"/custom/route/todoItems/{todoItem.Id}";
9494

@@ -114,7 +114,7 @@ public async Task CustomRouteControllers_Creates_Proper_Relationship_Links()
114114
context.TodoItems.Add(todoItem);
115115
await context.SaveChangesAsync();
116116

117-
var builder = new WebHostBuilder().UseStartup<Startup>();
117+
var builder = new WebHostBuilder().UseStartup<TestStartup>();
118118
var httpMethod = new HttpMethod("GET");
119119
var route = $"/custom/route/todoItems/{todoItem.Id}";
120120

@@ -139,7 +139,7 @@ public async Task CustomRouteControllers_Creates_Proper_Relationship_Links()
139139
public async Task ApiController_attribute_transforms_NotFound_action_result_without_arguments_into_ProblemDetails()
140140
{
141141
// Arrange
142-
var builder = new WebHostBuilder().UseStartup<Startup>();
142+
var builder = new WebHostBuilder().UseStartup<TestStartup>();
143143
var route = "/custom/route/todoItems/99999999";
144144

145145
var requestBody = new

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/IgnoreDefaultValuesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public sealed class IgnoreDefaultValuesTests : IAsyncLifetime
2121
private readonly AppDbContext _dbContext;
2222
private readonly TodoItem _todoItem;
2323

24-
public IgnoreDefaultValuesTests(TestFixture<Startup> fixture)
24+
public IgnoreDefaultValuesTests(TestFixture<TestStartup> fixture)
2525
{
2626
_dbContext = fixture.GetService<AppDbContext>();
2727
var todoItem = new TodoItem
@@ -90,7 +90,7 @@ public Task DisposeAsync()
9090
[InlineData(DefaultValueHandling.Include, true, "", null)]
9191
public async Task CheckBehaviorCombination(DefaultValueHandling? defaultValue, bool? allowQueryStringOverride, string queryStringValue, DefaultValueHandling? expected)
9292
{
93-
var builder = new WebHostBuilder().UseStartup<Startup>();
93+
var builder = new WebHostBuilder().UseStartup<TestStartup>();
9494
var server = new TestServer(builder);
9595
var services = server.Host.Services;
9696
var client = server.CreateClient();

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/IgnoreNullValuesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public sealed class IgnoreNullValuesTests : IAsyncLifetime
2121
private readonly AppDbContext _dbContext;
2222
private readonly TodoItem _todoItem;
2323

24-
public IgnoreNullValuesTests(TestFixture<Startup> fixture)
24+
public IgnoreNullValuesTests(TestFixture<TestStartup> fixture)
2525
{
2626
_dbContext = fixture.GetService<AppDbContext>();
2727
var todoItem = new TodoItem
@@ -93,7 +93,7 @@ public Task DisposeAsync()
9393
[InlineData(NullValueHandling.Include, true, "", null)]
9494
public async Task CheckBehaviorCombination(NullValueHandling? defaultValue, bool? allowQueryStringOverride, string queryStringValue, NullValueHandling? expected)
9595
{
96-
var builder = new WebHostBuilder().UseStartup<Startup>();
96+
var builder = new WebHostBuilder().UseStartup<TestStartup>();
9797
var server = new TestServer(builder);
9898
var services = server.Host.Services;
9999
var client = server.CreateClient();

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/RequestMetaTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility
1414
[Collection("WebHostCollection")]
1515
public sealed class RequestMetaTests
1616
{
17-
private readonly TestFixture<Startup> _fixture;
17+
private readonly TestFixture<TestStartup> _fixture;
1818

19-
public RequestMetaTests(TestFixture<Startup> fixture)
19+
public RequestMetaTests(TestFixture<TestStartup> fixture)
2020
{
2121
_fixture = fixture;
2222
}

0 commit comments

Comments
 (0)