【问题标题】:Unit/Integration tests for DAL and BLL (with lambdas)DAL 和 BLL 的单元/集成测试(使用 lambda)
【发布时间】:2015-11-07 22:00:40
【问题描述】:

我的代码基本上是这样的:

数据访问合同:

public interface IProvideDataAccess<T>
    where T : Entity
{
    IEnumerable<T> Select(Func<T, bool> condition);
    void Save(T entity);
    void Delete(T entity);
}

数据访问层:

public class Db4oProvider<T> : IProvideDataAccess<T>
    where T : Entity
{
    private IEmbeddedConfiguration _configuration;
    private string _connectionString;

    public Db4oAccesDonnees(string connectionString)
    {
        _connectionString = connectionString;
        _configuration = Db4oEmbedded.NewConfiguration();
    }

    IEnumerable<T> IProvideDataAccess<T>.Select(Func<T, bool> condition)
    {
        using (IObjectContainer db = Db4oEmbedded.OpenFile(_configuration, _connexion))
        {
            return db.Query<T>(e => condition(e));
        }
    }
    void IProvideDataAccess<T>.Save(T entity)
    {
        using (IObjectContainer db = Db4oEmbedded.OpenFile(_configuration, _connexion))
        {
            db.Store(entity);
        }
    }
    void IProvideDataAccess<T>.Delete(T entity)
    {
        using (IObjectContainer db = Db4oEmbedded.OpenFile(_configuration, _connexion))
        {
            db.Delete(entity);
        }
    }
}

业务逻辑层:

public class MyRepository
{
    protected IProvideDataAccess<MyEntityType> _dataAccessProvider;

    public MyRepository(IProvideDataAccess<MyEntityType> dataAccessProvider)
    {
        _dataAccessProvider = dataAccessProvider;
    }

    public IEnumerable<MyEntityType> SelectValidEntities()
    {
        return _dataAccessProvider.Select(e => /* test if entity is valid */);
    }
}

单元/集成测试对我来说相当新,我不知道从哪里开始。

认为合乎逻辑的做法是

  • 为 DAL 编写集成测试
  • 为 BLL 编写单元测试(使用假 DAL)

我说的对吗?

我最大的问题是“选择”方法及其 Func 参数。我如何测试/模拟它?

基本上,我应该为这两个类写什么测试?

【问题讨论】:

    标签: c# unit-testing mocking integration-testing data-access-layer


    【解决方案1】:

    您的DAL 看起来像永远不会改变的类...在我看来,在它们上创建UTIT 是浪费时间。您应该使用良好的组件/“大型集成”(不仅是单元+数据库)测试来涵盖类的行为,例如Db4oProvider&lt;T&gt;。我建议您阅读有关Test PyramidIn Martin Fowler blogthis good article)的信息。我喜欢下面的金字塔:

    关于Select 方法:

    Integration test(unit + DB) 中,您必须将数据放入您的 DB 中(或保留一个带有数据的 DB...),使用条件调用 Select,然后验证您是否获得了预期的数据.

    Component test 中,您需要将其作为被测行为的一部分进行测试,如果测试成功则一切正常...

    UT 中,测试分为两种类型:

    1.验证传递给Select 方法的条件(lambda)的行为的测试:(通过这种方式,您还可以验证Select 是否使用正确的参数调用...在我的示例中,我使用RhinoMocks)

    var fakeProvider = MockRepository.GenerateStub<IProvideDataAccess<Entity>>();
    
    fakeProvider.Select(o => o.Id =="asdfg");
    
    fakeProvider.AssertWasCalled(access => access.Select(Arg<Func<Entity, bool>>
                .Matches(func => func(new Entity()
                {
                    Id = "asdfg"
                }))));
    

    2.验证调用者方法行为的测试。在这些测试中,您应该配置您的 DAL 以返回一些重要的参数:

    _selectResult = new List<Entity>();
    fakeProvider.Stub(x => x.Select(Arg<Func<Entity, bool>>.Is.Anything))
                .Return(_selectResult);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2011-09-09
      • 2011-01-23
      • 2020-10-26
      • 1970-01-01
      • 2012-08-24
      相关资源
      最近更新 更多