jeudi 5 mai 2016

How to test action result in unit test?

I just finished writing my code , and actually i'm looking to learn how to test my controller methods .. but I tried and tried with no result .. so please can someone learn me how to test this 2 type of methods :

CategoriesController:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "Category_id,Category_name,Category_Description")] Category category)
    {
        if (ModelState.IsValid)
        {
            var Myurl = category.Category_name;
            var find_name = db.Categories.Where(p => p.UrlSlug == Myurl).Single();
            if (find_name != null)
            {
                ModelState.AddModelError(string.Empty, "The category is already exists");
                return View(category);
            }
            category.UrlSlug = Myurl.MakeUrlFriendly();

            db.Categories.Add(category);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(category);
    }

HomeController:

public ActionResult Index()
    {
        HomeViewModel vm = new HomeViewModel();  

        var Home = db.Books.OrderByDescending(x => x.Book_id).Take(12).ToList();
        vm.Book = Home.ToList();

    }

I created a unit test project and want to know , how I can test them and thanks ...

Aucun commentaire:

Enregistrer un commentaire