[英]Testing crud operations in SQLite, using the repository pattern
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)我添加了驱动程序 class,所以每个订单都有一个与之关联的驱动程序,并将驱动程序作为外键放在我的订单 class 中(所以我有一对一的关系)
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现在我正在尝试测试所有这些功能,所以我做了一些单元测试,但唯一通过的是 Add_customner 和 Add_drver 其他一切都失败了
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 = "jc@terminator.com"
};
// 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为了让它工作,它必须返回 true
Make sure your UOW using the same object as _context?确保您的 UOW 使用与 _context 相同的 object? Make sure when setting up mocks your database context it is injected and assigned same object to UOW and _context.确保在设置模拟数据库上下文时,它被注入并分配给 UOW 和 _context 相同的 object。 This might be the reason your read-only operations are passing but others are failing.这可能是您的只读操作通过但其他操作失败的原因。
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
.由于您将其命名为_context
,我假设它是您的测试 class 的实例变量。因此,每当测试运行时,都会创建一个新测试 object 并因此创建_context
。 To test your operations other than add, you should assume the _context
is empty.要测试除添加之外的其他操作,您应该假设_context
为空。 That means you should first add the driver and then test removing it.这意味着您应该首先添加驱动程序,然后再测试删除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.