Skip to content

Instantly share code, notes, and snippets.

@scottsauber
Last active October 16, 2017 15:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save scottsauber/70b165584d5b50a675228062573bbe25 to your computer and use it in GitHub Desktop.
Full TestServer and InMemory DB Test
public class ApplicationUsersControllerGetApplicationUser
{
private readonly ApplicationDbContext _context;
private readonly HttpClient _client;
public ApplicationUsersControllerGetApplicationUser()
{
var builder = new WebHostBuilder()
.UseEnvironment("Testing")
.UseStartup<Startup>();
var server = new TestServer(builder);
_context = server.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
_client = server.CreateClient();
}
[Fact]
public async Task DoesReturnNotFound_GivenUserDoesNotExist()
{
// Act
var response = await _client.GetAsync($"/api/ApplicationUsers/abc"); // No users with ID abc
// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
[Fact]
public async Task DoesReturnOk_GivenUserExists()
{
// Arrange
var user = new ApplicationUser
{
Id = "123",
Email = "test@test.com"
};
_context.Users.Add(user);
_context.SaveChanges();
// Act
var response = await _client.GetAsync($"/api/ApplicationUsers/{user.Id}");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string jsonResult = await response.Content.ReadAsStringAsync();
ApplicationUser userFromJson = JsonConvert.DeserializeObject<ApplicationUser>(jsonResult);
Assert.Equal(user.Id, userFromJson.Id);
Assert.Equal(user.Email, userFromJson.Email);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment