Home > Software engineering >  Testing crud operations in SQLite, using the repository pattern
Testing crud operations in SQLite, using the repository pattern

Time:02-16

I m testing an app that my teacher made. I added the driver class, so every order has a driver associated with it, and a put the Driver as a foreign key in my Order class (So I have one to one relationship)

 public class Order {

    [ForeignKey("Driver")]
    public int DriverId { get; set; }
    public int Id { get; set; }
    public List<Item> Items { get; set; }
}

public class Driver {

    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [Range(1, 5)]
    public int Rating { get; set; }
}

Then I went ahead and created the add, update and delete driver, and did the migration and update to the database.

Now I am tryin to test all this functionality, so I made some unit-test but the only ones that are passing are Add_customner and Add_drver everything else fails

So I am hoping to see if anyone can share some light

This are my test

   [Test]
    public void AddCustomer_ValidCustomer_ReturnsTrue() {
        // Arrange
        Customer customer = new Customer() {
            Id = 1,
            FirstName = "James",
            LastName = "Cameron",
            StreetAddress = "123 Sesame Street",
            City = "New York City",
            ZIP = "11111",
            State = "NY",
            PhoneNumber = "1111111111",
            Email = "[email protected]"

        };

        // Act

        var result = uow.AddCustomer(customer);

        // Assert

        Assert.IsTrue(result);

        var qCust = _context.Customers.Where(q => q.Id == 1).FirstOrDefault();
        Assert.AreSame(qCust, customer);


    }
    [Test]
    public void AddDriver_ValidDriver_ReturnsTrue() {
        // Arrange
        Driver driver = new Driver() {
            ID = 1,
            FirstName = "James",
            LastName = "Cameron",
            Rating = 4

        };

        // Act

        var result = uow.AddDriver(driver);

        // Assert

        Assert.IsTrue(result);

        var qDrive = _context.Drivers.Where(q => q.ID == 1).FirstOrDefault();
        Assert.AreSame(qDrive, driver);


    }
    [Test]
    public void RemoveDriver_ValidDriver_ReturnsTrue() {
        // Arrange
        Driver driver = new Driver() {
            ID = 1,
            FirstName = "James",
            LastName = "Cameron",
            Rating = 4

        };

        // Act

        var result = uow.RemoveDriver(driver);

        // Assert
        var Drivera = _context.Drivers;


        Assert.IsTrue(result);

        var qDrive = _context.Drivers.Remove(driver);
        Assert.AreNotEqual(Drivera, qDrive);


    }



    [Test]
    public void UpdateDriver_ValidDriver_ReturnsTrue() {
        Driver driver = new Driver() {
            ID = 1,
            FirstName = "James",
            LastName = "Cameron",
            Rating = 4
        };

        var result = uow.UpdateDriver(driver);

        var qDrive = _context.Drivers.Where(q => q.ID == 1).FirstOrDefault();
        Assert.AreSame(qDrive, driver);
    }
}

}

For it to work it must return true

CodePudding user response:

Make sure your UOW using the same object as _context? Make sure when setting up mocks your database context it is injected and assigned same object to UOW and _context. This might be the reason your read-only operations are passing but others are failing.

CodePudding user response:

Since you name it _context, I assume it is an instance variable of your test class. So whenever a test run, a new test object create and thus _context. To test your operations other than add, you should assume the _context is empty. That means you should first add the driver and then test removing it.

  • Related