'How do I write a TestMethod for Creating a Section in my Project. I have figured out how to make test for EditSection. I need help for CreateSection

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using University_Management.Controllers;
using University_Management.Models;
using Microsoft.AspNetCore.Mvc;
using System.Linq;


namespace MSTestProject
{
    [TestClass]
    public class TestSectionsEdit
    {
        [TestMethod]
        public async Task CheckEditReturnsView()
        {
            // Arrange
            using var dbContext = 
                     DbContextMocker.GetApplicationDbContext(nameof(CheckEditReturnsView));
            var controller = new SectionsController(dbContext);
            var editSecId = DbContextMocker.SeedData_Sections[0].SectionId;

            // Act
            var actionResult = await controller.Edit(editSecId);


            // Assert
            // (a) Check if the result of action is a view
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            System.Console.WriteLine("Completed (a)");

            var viewResult = actionResult as ViewResult;

            // (b) If definded, check if the returned view is named as Edit
            if (!string.IsNullOrEmpty(viewResult.ViewName))
            {
                Assert.AreEqual("Edit", viewResult.ViewName);
            }
            System.Console.WriteLine("Completed (b)");

            // (c) Check if the model returned  is an object of  the correct type.
            Assert.IsInstanceOfType(viewResult.Model,typeof(Section));
            System.Console.WriteLine("Completed (c)");

            var editSec = viewResult.Model as Section;

            // (d) Check if the Model returned by the Action Method to the View, is the correct type
            Assert.IsNotNull(editSec);
            System.Console.WriteLine("Completed (d)");

            // (e) Check if the data returned matches with the seeded data
            var expectedSec = DbContextMocker.SeedData_Sections
                                .SingleOrDefault(e => e.SectionId ==  editSecId);

            Assert.AreEqual<int>(expectedSec.SectionId, editSec.SectionId,$"Section Id does not match");
            Assert.AreEqual<string>(expectedSec.SectionName, editSec.SectionName,$"Section Id does not match");
            System.Console.WriteLine("Completed (e)");

        }
        [TestMethod]
        public async Task CheckEditUpdatesOk()
        {
            // Arrange
            using var dbContext = DbContextMocker.GetApplicationDbContext(nameof(CheckEditUpdatesOk));
            var controller = new SectionsController(dbContext);
            var editSecId = DbContextMocker.SeedData_Sections[0].SectionId;
            var editSecName = DbContextMocker.SeedData_Sections[0].SectionName;
            var changedSecName = editSecName.ToUpper();

            // Act

            var actionResult = await controller.Edit(editSecId);

            var editSec = (actionResult as ViewResult).Model as Section;
            editSec.SectionName = changedSecName;
            actionResult = await controller.Edit(editSecId, editSec);

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(RedirectToActionResult));
            Assert.AreEqual("Index", (actionResult as RedirectToActionResult).ActionName);

        }

This is the code for the Editing Section in Project. But I want to make Test for creating Section in my project.In this Edit one Seed Data is the mock data that i am using .But in create one I have to create a new object of section to test is it creating or not ? So I have written some code .But it's not working so I need your help and suggestions. I am attaching my code below.Thank You

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using University_Management.Controllers;
using University_Management.Models;
using Microsoft.AspNetCore.Mvc;
using System.Linq;



namespace MSTestProject
{
    [TestClass]
    public class TestSectionsCreate
    {
        [TestMethod]
        public async void CheckCreateReturnsView()
        {
            // Assert
            using var dbContext = DbContextMocker.GetApplicationDbContext(nameof(CheckCreateReturnsView));
            var controller = new SectionsController(dbContext);
            Section NewData =  new Section { SectionId = 10, SectionName = "Gujarati Section" } ;

            // Act
            var actionResult = controller.Create();
            var createSec = (actionResult as ViewResult).Model as Section ;
            actionResult = await controller.Create(NewData);
        

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(RedirectToActionResult));
            Assert.AreEqual("Index", (actionResult as RedirectToActionResult).ActionName);

        }
    }

}
   
 
    




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source