首先,您应该避免在测试类中使用database
字段。
这可能会干扰可能使用同一对象同时运行的其他测试,这些测试可能会也可能不会干扰您的结果(但可能会)。
您的假数据库是哪个?
字段database
还是database.Create()
的结果?
如果是后者,那么您的测试实际上只是测试您的伪造代码,而不是您的产品代码。
由于尚不清楚您使用的是自定义类型还是 bcl类型,我将继续假设您正在测试自定义类型。
如果database
,db
和table
实际上是 bcl类型,则不再阅读。
测试这些类没有意义。
话虽这么说,db.Execute()
到底是做什么的?
您当前正在同时测试多项,应该避免。
从创建表的测试开始,然后检查它是否在那里。
您还需要进行测试以确保没有引发异常。
[Test]
public void DefineTableDoesNotThrow() {
// arrange
var database = // create your instance here
var db = database.Create();
var table = null;
// act & assert
Assert.DoesNotThrow(() => table = db.DefineTable("IndexedTable"));
// clean up
database.DropTable("IndexedTable");
}
[Test]
public void DefineTable() {
// arrange
var database = // create your instance here
var db = database.Create();
// act
var table = db.DefineTable("IndexedTable");
// assert
// check if your table was created and exists.
// do not care about the actual columns in here.
// clean up
database.DropTable("IndexedTable");
}
接下来,您要测试是否按所需方式创建了列。
这是另一项功能,因此它应该是另一项测试。
就像DefineTable
一样,您需要确保它不会首先引发异常,然后再担心它是否可以正常工作。
[Test]
public void ColumnAddDoesNotThrow() {
// arrange
var database = // create your instance here
var db = database.Create();
var table = db.DefineTable("IndexedTable");
// act & assert
Assert.DoesNotThrow(() => table.Columns.Add("Id",FieldType.Guid,false,true));
// clean up
database.DropTable("IndexedTable");
}
[Test]
public void ColumnAdd() {
// arrange
var database = // create your instance here
var db = database.Create();
var table = db.DefineTable("IndexedTable");
// act
table.Columns.Add("Id",true);
// assert
// check if your table now has the correct column
// cleanup
database.DropTable("IndexedTable");
}
下一个最好的事情是使最后一个测试使用多个输入。
这使您可以通过添加另一行代码来测试具有相同代码的边缘情况。
用[TestCaseAttribute(...)]
装饰测试,然后将所需参数添加到测试方法中。
必须为table.Column.Add(...)
的每个可能的重载执行此操作。
不用说,table.Indices.Add(...)
也是如此。
本文链接:https://www.f2er.com/3147041.html