public async Task PostShouldCreateAttempt() { //Arrange var attempt = Fakers.Attempt.Generate(); var challengeId = Guid.NewGuid(); var attemptsRepository = A.Fake <IAttemptsRepository>(); A.CallTo(() => attemptsRepository.CreateAttemptAsync(A <ChallengeAttempt> .Ignored)).Returns(Task.FromResult(attempt)); A.CallTo(() => attemptsRepository.FindAttemptAsync(attempt.Id)).Returns(Task.FromResult(attempt)); using var factory = new JeevesWebApplicationFactory(services => services.SwapSingleton(provider => attemptsRepository)); using var client = factory.CreateClient(); var createAttemptRequest = new CreateAttempt() { Solution = new byte[0] }; //Act using var response = await client.PostAsync($"/api/v1/challenges/{challengeId}/attempts", JsonSerializer.Serialize(createAttemptRequest)); //Assert response.StatusCode.Should().Be(HttpStatusCode.Created); using var getResponse = await client.GetSuccessAsync($"/api/v1/challenges/{challengeId}/attempts/{attempt.Id}"); var actualAttempt = JsonSerializer.Deserialize <JeevesAttempt>(await getResponse.Content.ReadAsStringAsync()); actualAttempt.Should().BeEquivalentTo(attempt); }
public async Task <ActionResult> Create([FromRoute] Guid challengeId, [FromBody] CreateAttempt request) { var attempt = await _attemptsService.CreateAttemptAsync(new ChallengeAttempt { Solution = request.Solution }); return(CreatedAtAction(nameof(GetById), new { challengeId = challengeId, id = attempt.Id }, attempt)); }
public async Task PostShouldReturnCreatedStatusCode() { //Arrange var attempt = Fakers.Attempt.Generate(); var challengeId = Guid.NewGuid(); var attemptsRepository = A.Fake <IAttemptsRepository>(); A.CallTo(() => attemptsRepository.CreateAttemptAsync(A <ChallengeAttempt> .Ignored)).Returns(Task.FromResult(attempt)); using var factory = new JeevesWebApplicationFactory(services => services.SwapSingleton(provider => attemptsRepository)); using var client = factory.CreateClient(); var createAttemptRequest = new CreateAttempt() { Solution = new byte[0] }; //Act using var response = await client.PostAsync($"/api/v1/challenges/{challengeId}/attempts", JsonSerializer.Serialize(createAttemptRequest)); //Assert response.StatusCode.Should().Be(HttpStatusCode.Created); response.Headers.Location.PathAndQuery.Should().Be($"/api/v1/challenges/{challengeId}/attempts/{attempt.Id}"); }