简体   繁体   中英

Unit testing for adding record in DB

I am using unit testing first time.

I have created the class 'CAR' using entity framework. I have following code in my project

namespace EntityFrameworkExample
{
    class Program
    {
        static void Main(string[] args)
        {

          public static void AddCar()
          {

            SampleDBEntities db = new SampleDBEntities();
            Car car = new Car();
            car.Brand = "Ford";
            car.Model = "Mastang";
            db.Cars.AddObject(car);
            db.SaveChanges();
            Console.WriteLine("Record Saved");
            Console.ReadLine();
         }
      }
  } 
}

Now I want to perform unit testing on this method.

I have added unit test project as well added ref of above project to it... but I am confused abt how to call it ?

I have writen below code

namespace UnitTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {

        [TestMethod]
        public void AddCar()
        {

            SampleDBEntities e = new SampleDBEntities();


            //what to do next ? ////////////////////////////////

      }

    }
}

I am confused abt next step what should I write ?

You should put the AddCar-method outside the Main-method to be able to call it. I would make a new class for the Car-functionality, outside of the Program class. Then you can call that class without having to interact with the program in your test.

For unit testing, do "Arrange", "Act", "Assert" inside your test method.

Arrange:

var cars = new Cars();

Act:

cars.AddCar("Ford", "Mustang");

Assert:

//check that car is in the database
var myCar = cars.GetCar("Ford", "Mustang");
Assert.IsNotNull(myCar);

To be able to test it, you should wipe the database before each test run.

edit for clarification:

namespace EntityFrameworkExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var cars = new Cars();
            cars.AddCar("Ford", "Mustang");
        }

    } 

    public class Cars
    {
        public static void AddCar(string brand, string model)
        {
            SampleDBEntities db = new SampleDBEntities();
            Car car = new Car();
            car.Brand = brand;
            car.Model = model;
            db.Cars.AddObject(car);
            db.SaveChanges();
            Console.WriteLine("Record Saved");
            Console.ReadLine();
        }

        public static Car GetCar(brand, model)
        {
            using(var db = new SampleDBEntities())
            {
                var car = from car in db.Cars
                    where car.Brand == brand
                        && car.Model == model
                    select car;
                return car.FirstOrDefault();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM