-
Notifications
You must be signed in to change notification settings - Fork 2
/
RuntimeFakerUsingMoqTests.cs
87 lines (70 loc) · 2.77 KB
/
RuntimeFakerUsingMoqTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using DotNet.Core.Faker.Runtime.WebApi.Sample;
using DotNet.Core.Runtime.Faker.WebApi.Sample;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using NUnit.Framework;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using DotNet.Core.Runtime.Faker;
using DotNet.Core.Runtime.Faker.Moq;
namespace DotNet.Core.Faker.Runtime.Integration.Tests
{
public class RuntimeFakerUsingMoqTests
{
private DateTime registeredValue;
private static WebApplicationFactory<Startup> factory;
private static IServiceProvider serviceProvider;
private HttpClient cliente;
[OneTimeSetUp]
public void OneTimeSetUp()
{
registeredValue = new Bogus.Faker().Date.Future();
factory = new WebApplicationFactory<Startup>().WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services =>
{
services.AddServiceWithFaker<Clock>(mock => mock.Setup(x => x.Now()).Returns(registeredValue));
}));
serviceProvider = factory.Services;
}
[OneTimeTearDown]
public void OneTimeTearDown() => factory.Dispose();
[SetUp]
public void Setup() => cliente = factory.CreateClient();
[TearDown]
public void TearDown()
{
cliente.Dispose();
serviceProvider.ResetAllFakeChanges();
}
[Test]
public async Task ShouldGetRegisteredValueForClock()
{
var result = await cliente.GetAsync("time");
result.StatusCode.Should().Be(200);
var content = await result.Content.ReadAsStringAsync();
content.Should().Be($"{registeredValue:yyyy-MM-ddThh:mm:ss}");
}
[Test]
public async Task ShouldChangeClockImplementation()
{
var now = new Bogus.Faker().Date.Past();
serviceProvider.ChangeFake<Clock>(mock => mock.Setup(x => x.Now()).Returns(now));
var result = await cliente.GetAsync("time");
result.StatusCode.Should().Be(200);
var content = await result.Content.ReadAsStringAsync();
content.Should().Be($"{now:yyyy-MM-ddThh:mm:ss}");
}
[Test]
public async Task ShouldCleanChangesAndRestoreRegisteredValue()
{
serviceProvider.ChangeFake<Clock>(mock => mock.Setup(x => x.Now()).Returns(new Bogus.Faker().Date.Past()));
serviceProvider.ResetAllFakeChanges();
var result = await cliente.GetAsync("time");
result.StatusCode.Should().Be(200);
var content = await result.Content.ReadAsStringAsync();
content.Should().Be($"{registeredValue:yyyy-MM-ddThh:mm:ss}");
}
}
}