2
我想使用FakeItEasy将伪造的nHibernate会话注入到我的资源库中,然后返回在我的测试中预定义的对象列表。有没有人有这样的经验?用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;
}
预先感谢任何建议!
看起来不错!我会尽快尝试,但是我想知道使用nHibernate LINQ提供程序的情况,只是调用session.Query().ToList(); 。在这种情况下,FakeItEasy将因为扩展方法而失败。 –
jdscolam