Skip to content

Instantly share code, notes, and snippets.

@josephwoodward
Created May 22, 2020 00:28
Show Gist options
  • Save josephwoodward/171c89097d1f2c4b2cdca562b3451a10 to your computer and use it in GitHub Desktop.
Save josephwoodward/171c89097d1f2c4b2cdca562b3451a10 to your computer and use it in GitHub Desktop.
public class ControllerTests : IClassFixture<Fixture<Startup>>
{
private readonly Fixture<Startup> _fixture;
public ControllerTests(Fixture<Startup> fixture) => _fixture = fixture;
[Fact]
public async Task ReturnsOrganizationRepositories()
{
// Arrange
using var _ = _fixture.InterceptorOptions.BeginScope();
new HttpRequestInterceptionBuilder()
.Requests()
.ForHttps()
.ForHost("api.github.com")
.ForPath("orgs/weyland-yutani/repos")
.ForQuery("per_page=10")
.Responds()
.WithSystemTextJsonContent(new[] {new {id = 1, name = "foo"}, new {id = 2, name = "bar"}})
.RegisterWith(_fixture.InterceptorOptions);
// Act
using var httpClient = _fixture.CreateClient();
string json = await httpClient.GetStringAsync("api/repos");
var actual = JsonConvert.DeserializeObject<string[]>(json);
// Assert
actual.ShouldBe(new[] {"bar", "foo"});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment