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 Create(Customer customer) { if (ModelState.IsValid) { _db.Add(customer); await _db.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(customer); }

//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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!