Skip to content

Commit

Permalink
WebApp sample refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed Jan 30, 2022
1 parent b58b5c9 commit 4a13238
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions test/DivertR.WebAppTests/WebAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ namespace DivertR.WebAppTests
{
public class WebAppTests : IClassFixture<WebAppFixture>
{
private readonly IVia<IFooRepository> _fooRepositoryVia;
private readonly IDiverter _diverter;
private readonly IFooClient _fooClient;
private readonly IServiceProvider _services;

public WebAppTests(WebAppFixture webAppFixture, ITestOutputHelper output)
{
var diverter = webAppFixture.InitDiverter(output);
_fooRepositoryVia = diverter.Via<IFooRepository>();
_diverter = webAppFixture.InitDiverter(output);
_fooClient = webAppFixture.CreateFooClient();
_services = webAppFixture.Services;
}
Expand All @@ -34,7 +33,8 @@ public async Task GivenFooExistsInRepo_WhenGetFoo_ThenReturnFooContent_WithOk200
Name = Guid.NewGuid().ToString()
};

_fooRepositoryVia
_diverter
.Via<IFooRepository>()
.To(x => x.GetFooAsync(foo.Id))
.Redirect(Task.FromResult(foo));

Expand All @@ -61,7 +61,8 @@ public async Task GivenFooExists_WhenGetFoo_ThenReadsFromFooRepository()

(Guid fooId, Foo foo) fooRepoCall = default;

_fooRepositoryVia
_diverter
.Via<IFooRepository>()
.To(x => x.GetFooAsync(Is<Guid>.Any))
.Redirect<(Guid fooId, __)>(async (call, args) =>
{
Expand Down Expand Up @@ -89,7 +90,8 @@ public async Task GivenFooDoesNotExist_WhenGetFoo_ThenReturn404NotFound()
Id = Guid.NewGuid()
};

var getFooCalls = _fooRepositoryVia
var getFooCalls = _diverter
.Via<IFooRepository>()
.To(x => x.GetFooAsync(Is<Guid>.Any))
.Redirect<(Guid fooId, __)>(Task.FromResult<Foo>(null))
.Record();
Expand Down Expand Up @@ -120,7 +122,8 @@ public async Task GiveFooNotExists_WhenCreateFooRequest_ThenInsertsFooAndReturn2
Foo insertedFoo = null;
bool? insertResult = null;

_fooRepositoryVia
_diverter
.Via<IFooRepository>()
.Strict()
.To(x => x.TryInsertFooAsync(Is<Foo>.Any))
.Redirect<(Foo foo, __)>(async (call, args) =>
Expand Down Expand Up @@ -150,7 +153,8 @@ public async Task GiveFooNotExists_WhenCreateFooRequest_ThenInsertsFoo_RecordExa
Name = Guid.NewGuid().ToString()
};

var insertCalls = _fooRepositoryVia
var insertCalls = _diverter
.Via<IFooRepository>()
.To(x => x.TryInsertFooAsync(Is<Foo>.Any))
.Record<(Foo foo, __)>();

Expand All @@ -175,7 +179,8 @@ public async Task GiveFooNotExists_WhenCreateFooRequest_ThenInsertsFoo_RecordMap
Name = Guid.NewGuid().ToString()
};

var insertCalls = _fooRepositoryVia
var insertCalls = _diverter
.Via<IFooRepository>()
.To(x => x.TryInsertFooAsync(Is<Foo>.Any))
.Record<(Foo foo, __)>()
.Map((call, args) => new
Expand Down Expand Up @@ -206,10 +211,11 @@ public async Task GivenFooRepositoryInsertFails_WhenCreateFooRequest_ThenReturns

var testException = new Exception("test");

var recordedCalls = _fooRepositoryVia
var recordedCalls = _diverter
.Via<IFooRepository>()
.To(x => x.TryInsertFooAsync(Is<Foo>.Any))
.Redirect(() => throw testException)
.Record<(Foo foo, __)>();
.Redirect<(Foo foo, __)>(() => throw testException)
.Record();

// ACT
var response = await _fooClient.CreateFooAsync(createFooRequest);
Expand Down

0 comments on commit 4a13238

Please sign in to comment.