Roll back refacto on Posit.Tests
This commit is contained in:
parent
06672c4c90
commit
88461786ee
15 changed files with 431 additions and 499 deletions
66
src/PostIt.Tests/BlogApiTestFakes.cs
Normal file
66
src/PostIt.Tests/BlogApiTestFakes.cs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
using Yavsc.Blogspot;
|
||||
using PostIt.Services;
|
||||
using PostIt.ViewModels;
|
||||
using Yavsc.Models;
|
||||
|
||||
namespace PostIt.Tests;
|
||||
|
||||
/// <summary>Per-call ledger shared between the test and the
|
||||
/// recording fake, so the assertion can inspect what the VM
|
||||
/// actually sent on the wire without coupling to the fake's
|
||||
/// internals.</summary>
|
||||
internal sealed class CallRecorder
|
||||
{
|
||||
public (HttpMethod method, string path, object? body) FirstCall =>
|
||||
Calls[0];
|
||||
public List<(HttpMethod method, string path, object? body)> Calls { get; } = new();
|
||||
}
|
||||
|
||||
/// <summary>Test fake that records every CallAsync invocation
|
||||
/// and answers them with a canned sequence: the first call gets
|
||||
/// a server-issued BlogPostDto (Id=42), the second call gets a
|
||||
/// single-element list containing that post. Used by the ViewModel
|
||||
/// tests and the headless UI test to capture exactly what the
|
||||
/// Save button posts to the server.</summary>
|
||||
internal sealed class RecordingYavscApiClient : YavscApiClient
|
||||
{
|
||||
private readonly CallRecorder _recorder;
|
||||
public RecordingYavscApiClient(CallRecorder recorder)
|
||||
: base(
|
||||
new Settings
|
||||
{
|
||||
Authentication = new AuthenticationSettings
|
||||
{
|
||||
Authority = "https://stub.invalid",
|
||||
ClientId = "stub",
|
||||
Scopes = new[] { "openid" },
|
||||
},
|
||||
},
|
||||
new TokenStore(System.IO.Path.GetTempFileName()))
|
||||
{
|
||||
_recorder = recorder;
|
||||
}
|
||||
|
||||
public override Task<T> CallAsync<T>(HttpMethod method, string path, object? body = null, CancellationToken ct = default)
|
||||
{
|
||||
_recorder.Calls.Add((method, path, body));
|
||||
// BlogPostDto? boxes to BlogPostDto at runtime, so we test the
|
||||
// non-nullable type — typeof(BlogPostDto?) is a C# error
|
||||
// (CS8639: "typeof cannot be used on a nullable reference
|
||||
// type").
|
||||
if (typeof(T) == typeof(BlogPostDto))
|
||||
return Task.FromResult((T)(object)new BlogPostDto
|
||||
{
|
||||
Id = 42,
|
||||
Title = "Mon premier billet",
|
||||
AuthorId = "tester",
|
||||
Article = "Contenu du billet de test.",
|
||||
});
|
||||
if (typeof(T) == typeof(List<BlogPostDto>))
|
||||
return Task.FromResult((T)(object)new List<BlogPostDto>
|
||||
{
|
||||
new() { Id = 42, Title = "Mon premier billet" }
|
||||
});
|
||||
return Task.FromResult(default(T)!);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue