开发者

How do I verify that a collection is correctly fetched from db?

I am trying to write an integration test for my repository, but I can't find a nice way to assert that the results are what I want them to.

I have a class with static properties for all my test data - Projects, Users etc. In the setup of my test, I insert all these entities in my database 开发者_开发知识库using NHibernate (and I've been able to verify that this works - the records are there as they should after the setup is complete).

My repository (slightly simplified) does this:

public IEnumerable<MyEntity> GetEntitiesByProjectID(id)
{
    return session.Linq<MyEntity>().Where(e => e.Project.ID == id).ToList();
}

In my test, I try to assert that the results are correct as in the code example below, but the collections are not equivalent for several reasons - the primary one being that referenced other entites are replaced with castle proxies (Castle.Proxies.UserProxy instead of User).

Assert.AreElementsEqualIgnoreOrder(
    Data.MyEntites.Where(e => e.Project.ID == 1), results)

There are some other oddities too - for example the way datetimes are represented. In my .NET types, the datetime string representations are 2011-07-05T14:28:11.5655935+02:00, while the ones coming from the db are 2011-07-05T14:28:11.0000000, i.e. without the timezone indicator.

By inspection I can verify that the repository has really fetched the right elements, so my test is obviously testing the wrong thing.

What am I doing wrong here?


Are you sure than you have to test data layer? I believe NHIbernate was well tested and you have to test you service layer and provide mocks for data.

If it's not a case, set lazy loading to false during your integration testing, it should help resolve UserProxy instead of User issue. For datetime issue, please check what data type has a column in database, there are: datetime, datetime2, date... check if you column support timezones

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜