Question: ** IF you do not know how to answer this question to not spam answer me like the last chegg expert . Let a chegg

** IF you do not know how to answer this question to not spam answer me like the last chegg "expert" . Let a chegg expert answer it who actually knows how to answer it

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 "VendorAddControler.cs"

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using CS4790FinalProject3.Data; using CS4790FinalProject3.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore;

namespace CS4790FinalProject3.Controllers { public class VendorAddProductController : Controller { // get the database object private readonly ApplicationDbContext _db; //now add a constructor public VendorAddProductController(ApplicationDbContext db) { _db = db; } public IActionResult Index() { //NEEDS TO RETURN WHERE EQUAL TO SESSION return View(_db.Products.ToList()); }

public IActionResult Create() { return View(); } //post action for create [HttpPost] [ValidateAntiForgeryToken] public async Task Create(VendorAddProduct vendorAddProduct) { if (ModelState.IsValid) { _db.Add(vendorAddProduct); await _db.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(vendorAddProduct); }

//Details : VendorAddProducts/Details/3 public async Task Details(int? id) { if (id == null) { return NotFound(); } var product = await _db.Products.SingleOrDefaultAsync(m => m.Id == id); //we will now have the object from the database if (product == null) { return NotFound(); } return View(product); }

//Edit : VendorAddProducts/Details/3 public async Task Edit(int? id) { if (id == null) { return NotFound(); } var product = await _db.Products.SingleOrDefaultAsync(m => m.Id == id); //we will now have the object from the database if (product == null) { return NotFound(); } return View(product); }

//This will be for the post of Edit [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(int id, VendorAddProduct product) { if (id != product.Id) { return NotFound(); } if (ModelState.IsValid) { _db.Update(product); await _db.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(product); }

//Details : VendorAddProducts/Details/3 //this is actually a get method public async Task Delete(int? id) { if (id == null) { return NotFound(); } var product = await _db.Products.SingleOrDefaultAsync(m => m.Id == id); //we will now have the object from the database if (product == null) { return NotFound(); } return View(product); }

//POST : delete the book ->the action name os refering to the public asyn method of Delete at the bottom. it can be used if you want to call it something else [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task RemoveProduct(int id) { var product = await _db.Products.SingleOrDefaultAsync(m => m.Id == id); _db.Products.Remove(product); await _db.SaveChangesAsync(); return RedirectToAction(nameof(Index));

}

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!