Question Unit Testing with EF on MVC 5

Niso

New member
Joined
Nov 30, 2020
Messages
3
Programming Experience
1-3
hello,
im having trouble connecting my DB to the unit testing. i saw a video on youtube that recommended moq and adding repositorys, but i dont think im suppose to get that far.
i just want to write some simple tests to see if they are added to the db as expected.
im adding the code for my controller and the testing unit. i know the code is not fully implemented but i was just trying to see if i can reach the DB through the test project (i was just trying to reach)

C#:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using VolunteesMVC6.Models;


namespace VolunteesMVC6.Controllers
{
    public class HomeController : Controller
    {
        string xml = @"C:\Users\nisan\Desktop\Niso\תכנות\VolunteesMVC6\VolunteesMVC6\obj\Debug\cities.xml";
        
        public ActionResult Index()
        {
            ds.ReadXml(xml);
            return View();
        }

        public ActionResult SignUp()
        {
            //a veriable class that EF created from SQL   
            tb_Users users = new tb_Users();
            
           if (ds.Tables.Count==0)
            {
                ds.ReadXml(xml);
            }

            string[] names=GetAllCities();
 
            Array.Sort(names);
            ViewBag.CitiesList = new SelectList(names);
            
            return View(users);
        }
        DataSet ds = new DataSet();
        public string[] GetAllCities()
        {

            return (from row in ds.Tables[0].AsEnumerable()
                    select row.Field<string>("strCity")).ToArray();

        }

    }
}

the unit test project with the error:
5123.PNG


i cant understand what the problem is and would love an explanation on how to make it work.
please help!!
 
If you are trying to test if you are successfully writing to a database, then you are doing an integration test, not a unit test. With a unit test, you would pass in a mock object that looks like the database interface and your unit test would verify if the appropriate calls to write data into the database are being called by asserting that the mock objects methods were called.

Anyway with regard to your problem, the exception information on the right side of your screenshot is telling you exactly what the problem is. The unit test engine is calling your controller, but the controller is unable to create the view because the view engine couldn't find some of the other assemblies it needs. You'll need to somehow tell the view engine to make those other assemblies available to your code to be tested.
 
The tests you are trying to do are not suitable for tests on DB you need integration tests,
You can use with something called moq.
However, the problem presented during the unit test You'll need to somehow tell the view engine to make those other assemblies available to your code to be tested.
 
Last edited:
Back
Top Bottom