简体   繁体   English

单元测试一种创建带有索引的表的方法

[英]Unit Testing a method that creates a table with indexes

I created this fake db for the test, but i don't know how to continue from here.我为测试创建了这个假数据库,但我不知道如何从这里继续。 I know that i need to create expected scenarios with yield return and compare the results with expected resulsts and so forth but i can't figure out how to start, I just need a starting point.我知道我需要创建具有收益率回报的预期场景并将结果与预期结果进行比较等等,但我不知道如何开始,我只需要一个起点。 This is all new to me, so apologies from everyone if this is a dumb question.这对我来说是全新的,所以如果这是一个愚蠢的问题,请向大家道歉。

public class IndexTests : DataAccessFixture
{
    [Test]
    public void CreateTableWithIndex()
    {
        var db = this.database.Create();
        var table = db.DefineTable("IndexedTable");

        table.Columns.Add("Id", FieldType.Guid, false, true);
        table.Columns.Add("CustomerNumber", FieldType.VarChar, 50);
        table.Columns.Add("Name", FieldType.VarChar, 50);

        table.Indices.Add("IX_Name", false, "Name");
        table.Indices.Add("IX_CustomerNumber", true, "CustomerNumber");

        db.Execute();

        this.database.DropTable(table.Name);
    }
}

First of all you should avoid having a database field in your test class.首先,您应该避免在测试 class 中使用database字段。 This can interfere with other tests that may be running concurrently using the same object which may or may not mess with your results (but probably will).这可能会干扰可能使用相同的 object 同时运行的其他测试,这可能会或可能不会干扰您的结果(但可能会)。

Which one is your fake db?哪个是你的假数据库? The field database or the results from database.Create() ?字段database或来自database.Create()的结果? If the latter is the case then your test is really just testing your fake code and not your product code.如果是后者,那么您的测试实际上只是测试您的假代码而不是您的产品代码。

Since it is not clear if you are using custom types or bcl types i'll just go ahead and assume that you are testing your custom types.由于尚不清楚您使用的是自定义类型还是bcl 类型,所以我将只是 go 并假设您正在测试您的自定义类型。 If database , db and table are in fact bcl types then read no further.如果databasedbtable实际上是bcl 类型,则无需进一步阅读。 There is no point in testing those classes.测试这些类没有意义。 That being said, what exactly does db.Execute() do?话虽这么说, db.Execute()究竟做了什么?

You are currently testing multiple things at the same time which should be avoided.您目前正在同时测试多个事物,这应该避免。 Start with a test that creates the table and check if it's there.从创建表的测试开始并检查它是否存在。 You also need a test that makes sure there is no exception thrown.您还需要一个确保没有抛出异常的测试。

[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");

}

Next you want to test if your columns are created the way you want it.接下来,您要测试您的列是否按照您想要的方式创建。 This is another functionality so it should be another test.这是另一个功能,所以它应该是另一个测试。 It works just like DefineTable where you want to make sure it doesn't throw exceptions first and then you worry about if it worked correctly.它就像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", FieldType.Guid, false, true);

    // assert
    // check if your table now has the correct column

    // cleanup
    database.DropTable("IndexedTable");

}

The next best thing to do is make that last test use multiple inputs.下一个最好的事情是让最后一个测试使用多个输入。 This allows you to test edge cases with the same code by just adding another line of code.这允许您通过添加另一行代码来使用相同的代码测试边缘情况。 Decorate the test with [TestCaseAttribute(...)] and add the required parameters to your test method.使用[TestCaseAttribute(...)]装饰测试并将所需的参数添加到您的测试方法中。 This has to be done for every possible overload of table.Column.Add(...) .对于table.Column.Add(...)的每一个可能的重载,都必须这样做。 Needless to say that this goes for table.Indices.Add(...) just as well.不用说,这也适用于table.Indices.Add(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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