使用 FakeItEasy 伪造 nHibernate 会话

发布于 2024-11-16 13:08:46 字数 1078 浏览 2 评论 0原文

我想使用 FakeItEasy 将一个假的 nHibernate 会话注入到我的存储库中,然后返回在我的测试中预定义的对象列表。有人有这样做的经验吗?

这是示例测试:

[TestFixture]
public class ProductionRepositoryTester
{
 private ProductionRepository _productionRepository;

[SetUp]
public void SetupFixture()
{
    const string propertyNumber = "123";
    Tank tank = new Tank { PropertyNumber = propertyNumber };

    var session = A.Fake<ISession>();
    var sessionFactory = A.Fake<ISessionFactory>();

    A.CallTo(session).WithReturnType<IList<Tank>>().Returns(new List<Tank> { tank });

    _productionRepository = new ProductionRepository(session, sessionFactory);
}

[Test]
public void ProductionRepositoryCanGetTanks()
{
    var tanks = _productionRepository.GetTanks();

    Assert.AreNotEqual(0, tanks.Count(), "Tanks should have been returned.");

}
}

这是实际 ProductionRepository 类中的调用:

public IEnumerable<Tank> GetTanks()
{
    var tanks = Session.CreateCriteria(typeof(Tank)).List<Tank>();
    return tanks;
}

提前感谢您的任何建议!

I would like to inject a fake nHibernate session into my repository using FakeItEasy, then return a list of objects that are predefined within my test. Does anyone have experience doing this?

Here is the example test:

[TestFixture]
public class ProductionRepositoryTester
{
 private ProductionRepository _productionRepository;

[SetUp]
public void SetupFixture()
{
    const string propertyNumber = "123";
    Tank tank = new Tank { PropertyNumber = propertyNumber };

    var session = A.Fake<ISession>();
    var sessionFactory = A.Fake<ISessionFactory>();

    A.CallTo(session).WithReturnType<IList<Tank>>().Returns(new List<Tank> { tank });

    _productionRepository = new ProductionRepository(session, sessionFactory);
}

[Test]
public void ProductionRepositoryCanGetTanks()
{
    var tanks = _productionRepository.GetTanks();

    Assert.AreNotEqual(0, tanks.Count(), "Tanks should have been returned.");

}
}

And here is the call within the actual ProductionRepository class:

public IEnumerable<Tank> GetTanks()
{
    var tanks = Session.CreateCriteria(typeof(Tank)).List<Tank>();
    return tanks;
}

Thanks in advance for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-11-23 13:08:46

首先,我建议不要伪造 NHibernate 接口,(在我看来)这对于单元测试来说太低了。针对这些场景进行一些集成测试可能会更好。换句话说,对与 ProductionRepository (IProductionRepository) 抽象的所有交互进行单元测试,但到此为止。然而,现在这只是我的意见,如果你真的想这样做,我认为你必须改变你的假设置:

会话返回一个标准,而不是直接返回一个 IList。因此,你也必须有一个假标准:(

var fakeCriteria = A.Fake<ICriteria>();

A.CallTo(fakeCriteria).WithReturnType<IList<Tank>>().Returns(new List<Tank> { tank });

A.CallTo(session).WithReturnType<ICriteria>().Returns(fakeCriteria);

我希望我正确地记住了标准类型,我认为它是 ICriteria,但我不是百分百确定。)

First of all I would advice against faking the NHibernate interfaces at all, this is (in my opinion) too low a level to unit test. It's probably better to have some integration tests for these scenarios. In other words, unit test all interaction with an abstraction for ProductionRepository (IProductionRepository) but stop there. Now, however, that's just my opinion and if you really want to do this I think you would have to change your fake-setup:

The session returns a criteria, not ever a IList directly. Therefore you'd have to have a fake criteria too:

var fakeCriteria = A.Fake<ICriteria>();

A.CallTo(fakeCriteria).WithReturnType<IList<Tank>>().Returns(new List<Tank> { tank });

A.CallTo(session).WithReturnType<ICriteria>().Returns(fakeCriteria);

(I hope I remember the criteria type correctly, I think it's ICriteria but I'm not a hundred percent sure.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文