mardi 8 décembre 2015

How to unit test a method to check if it saves to the database?

I need to unit test to check if the Create method saves to the database. I know that this method should not be unit tested in a real project. I am doing this just for learning and demonstration purposes in a school project. I know that I have to use mocking objects and some abstract dependencies of the controller as I read in some other answers, but because I just started learning c# this doesn't help me too much. Can you please show me a simple solution or some advice. Thanks.

This is the Create method:

[HttpPost]
[ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,Code,Description")] LocationCode locationCode)
    {
        if (ModelState.IsValid)
        {
            uow.LocationCodeRepository.Insert(locationCode);

            return RedirectToAction("Index");
        }

        return View(locationCode);
    }

This is the LocationCode class from the Model:



using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace BookInventory.Models
{
    public class LocationCode
    {
        [Key]
        public int Id { get; set; }

        public string Code { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Book> Books { get; set; }
    }
}

Aucun commentaire:

Enregistrer un commentaire