单元测试不返回连接表的结果

时间:2016-07-03 11:27:30

标签: c# unit-testing mspec simple.data

我鼓励在编写单元测试时遇到问题,该单元测试检查函数在连接两个表时是否返回有效结果。我有以下场景: 我有2个表,命名为Folder和Item,Folder可以有多个项目。他们的DDL看起来像这样:

create table Folder
(   varchar(max) Name,
    int User_Id,
    int Id primary key
);
create table Item
(   int Id primary key,
    varchar(max) Data,
    int User_Id,
    int Folder_id foreign key Folder(Id)
}

我想从属于名称以某些前缀开头的文件夹的项目中收集所有“数据”。所以我编写了以下函数,该函数工作正常并返回有效结果。

string GetDatas(string prefix, int userId) //_db is injected, this function is a part of a DatasProvider class
{
    dynamic folder;
    return _db.dbo.Item.FindAllByUser_Id(userId)
            .Join(_db.dbo.Folder, out folder)
            .On(_db.dbo.Item.Folder_id == folder.Id)
            .Where(_db.dbo.Folder.Name.Like(prefix))
            .Select(_db.dbo.Item.Data)
            .ToScalarList<string>();
}

我编写了一个单元测试来检查函数是否正常工作,我的规范如下:

public class Spec
{
    private static dynamic _db;
    private static IList<string> result;
    private const int userId = 14;
    private const string validPrefix = "dd";
    private const int validFolderId = 22;
    private const int invalidFolderId = 33;

    private static readonly List<string> validData = new List<string>()
    {
        "1",
        "2",
        "3",
        "4"
    };

    private static readonly List<string> invalidData = new List<string>()
    {
        "a",
        "b",
        "z"
    };

    public class When_getting_data : Observes<DatasProvider>
    {
        public Establish context = () =>
        {
            Database.UseMockAdapter(new InMemoryAdapter());
            _db = Database.Open();
            _db.dbo.Folder.Insert(Id: validFolderId, User_Id: userId, Name: validPrefix + "4231142");
            _db.dbo.Folder.Insert(Id: invalidFolderId, User_Id: userId, Name: "4321" + validPrefix + "4231142");

            for (int i = 0; i < validData.Count; ++i)
            {
                _db.dbo.Item.Insert(Id: i, User_Id: userId, Folder_Id: validFolderId, Data: validData[i]);
            }
            for (int i = 0; i < invalidData.Count; ++i)
            {
                _db.dbo.Item.Insert(Id: i + validData.Count, User_Id: userId, Folder_Id: invalidFolderId, Data: invalidData[i]);
            }
            depends.on<dynamic>(_db);
        };

        private Because of = () => result = sut.GetDatas(userId, validPrefix);
        private It should_return_valid_datas_count = () => result.Count.ShouldEqual(validData.Count);

        private It should_return_valid_datas = () => result.ShouldContainOnly(validData);
        private It should_not_return_invalid_datas = () => result.ShouldNotContain(invalidData);
    }
}

但规格失败因为函数GetDatas返回0结果。当我对debbug代码进行debbug时,我发现它为用户找到了所有项目,但是当我想获得与属于用户的这些项目相关联的所有文件夹名称时,我得到一个包含有效结果数量的列表,但是具有空值每个结果的内部..我应该做些额外的事情来让测试按照它应该的方式工作吗?

修改

我也尝试在db adapter中指定master / detail,但它什么也没做。

var adapter = new InMemoryAdapter();
adapter.Join.Master("Folder", "Id").Detail("Item", "Folder_Id");
Database.UseMockAdapter(adapter);

1 个答案:

答案 0 :(得分:0)

我通过更改部分代码来收集数据来解决我的问题:

dynamic folder;
return _db.Item.FindAllByUser_Id(userId)
    .Join(_db.Folder, out folder)
    .On(_db.Item.Folder_Id == folder.Id)
    .With(folder)
    .Where(folder.Name.Like(prefix))
    .Select(_db.Item.Data)
    .ToScalarList<string>();

并通过在测试中指定具有关系的适配器

var adapter = new InMemoryAdapter();
adapter.Join.Master("Folder", "Id").Detail("Item", "Folder_Id");
Database.UseMockAdapter(adapter);
_db = Database.Open();