Question: I need to learn how to write a Unit Test for a .NET CORE MVC . I'm using microsoft visual studios and using Xunit. I
I need to learn how to write a Unit Test for a .NET CORE MVC . I'm using microsoft visual studios and using Xunit. I need to write a unit test to test a CONTROLLER in the mvc project.
CONTROLLER TO TEST IS CALLED "CustomerAddControler.cs"
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using CS4790FinalProject3.Data; using CS4790FinalProject3.Models; using Microsoft.AspNetCore.Mvc;
namespace CS4790FinalProject3.Controllers { public class CustomerAddController : Controller { //get Database object to retrive or make changes to the database private readonly ApplicationDbContext _db; //initialize _db in a constructor
public CustomerAddController(ApplicationDbContext db) { _db = db; }
//at the index of load of the page it will display all customers public IActionResult Index() { // want to get all things from the database return View(_db.Customer.ToList()); // right click on Index to create a "add view } //now we need an action result of create a customer public IActionResult Create() { return View(); } //now create a post action for create [HttpPost] [ValidateAntiForgeryToken] public async Task
//after its useage we need to be able to dispose.. not sure why
protected override void Dispose(bool disposing) { if (disposing) { _db.Dispose(); } }
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
