public async Task TestCreate(string modelName, string pdfName, int?page, string expectedResultName = null) { //dbContext will be created with 0 records using var dbContext = DBContextMocker.GetContext(nameof(TestCreate), 0); // Arrange var record = new FieldNaming { ModelFieldName = modelName, PdfFieldName = pdfName }; //This simulates when user don't fill difficulty field in the JSON that is sent if (page is int intPage) { record.Page = intPage; } var controller = new FieldNamingsController(dbContext); // Act ActionResult <FieldNaming> response = null; // Mimic the behaviour of the model binder which is responsible for Validating the Model if (controller.Validate(record)) { response = await controller.PostFieldNaming(record); } // Assert Assert.True(response?.Result.GetType().Name == expectedResultName, expectedResultName is null ? "It was expected that this test case would not pass the model biding validation, however it did." : $"It was expected that this test case would have a '{expectedResultName}' return, however it did not."); }
public async Task TestUpdate(string id, string jsonId, string expectedResultName) { //dbContext will be created using var dbContext = DBContextMocker.GetContext($"{nameof(TestUpdate)}_{id}_{jsonId}", TOTAL_RECORDS); // Arrange var record = new FieldNaming { ModelFieldName = FIELD_NAME, PdfFieldName = PDF_NAME, Page = 1 }; dbContext.FieldNaming.Add(record); dbContext.SaveChanges(); //Changing data for update record.PdfFieldName = "Changed Name"; record.ModelFieldName = jsonId; var controller = new FieldNamingsController(dbContext); // Act var response = await controller.PutFieldNaming(id, record); // Assert Assert.True(response.GetType().Name == expectedResultName, $"It was expected that this test case would have a '{expectedResultName}' return, however it did not. Actual result type: {response.GetType()}."); }
public async Task Create_EndpointReturnSuccessAndCorrectContentType() { try { // Arrange var record = new FieldNaming { ModelFieldName = FIELD_NAME, PdfFieldName = PDF_NAME, Page = 1 }; var response = await this.Client.PostAsync("/api/FieldNamings", record.GetStringContent()); // Assert Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); var returnedRecord = response.ContentAsType <FieldNaming>(); Assert.Equal(record.ModelFieldName, returnedRecord.ModelFieldName); Assert.Equal(record.PdfFieldName, returnedRecord.PdfFieldName); Assert.Equal(record.Page, returnedRecord.Page); } finally { //Clear Data await ClearAll(); } }
//This is necessary in order to test validations in Unit Tests. Integrations tests however do this automatically with Http Requests public static bool Validate(this ControllerBase controller, FieldNaming model) { var validationContext = new ValidationContext(model, null, null); var validationResults = new List <ValidationResult>(); Validator.TryValidateObject(model, validationContext, validationResults, true); foreach (var validationResult in validationResults) { controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage); } return(controller.ModelState.IsValid); }
public async Task Update_EndpointReturnNotFound() { // Arrange var record = new FieldNaming { ModelFieldName = FIELD_NAME, PdfFieldName = PDF_NAME, Page = 1 }; //Act var response = await(this.Client.PutAsync($"api/FieldNamings/{FIELD_NAME}", record.GetStringContent())); // Assert Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }