DAL和BLL的单元/集成测试(带有lambda) [英] Unit/Integration tests for DAL and BLL (with lambdas)

查看:108
本文介绍了DAL和BLL的单元/集成测试(带有lambda)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My code basically looks like this:

数据访问合同:

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 */);
    }
}

单元/集成测试对我来说还很新,我不确定从哪里开始.

Unit/Integration tests are fairly new to me and I'm not sure where to begin.

认为要做的合理的事情是

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

我说得对吗?

我最大的问题是"select"方法及其Func参数.我该如何测试/模拟呢?

My biggest problem is with the "select" method and its Func parameter. How do I test/mock that?

基本上,我应该为这两个类编写哪些测试?

Basically, what tests should I write for these two classes?

推荐答案

您的DAL看起来就像是永远不变的类...在我看来,在它们上创建UTIT是浪费时间. .您应该通过良好的组件/大型集成"(不仅是单元+ DB)测试来涵盖类Db4oProvider<T>的行为.我提供给您阅读有关Test Pyramid(在Martin Fowler博客中

Your DAL looks like the classes which never change... In my opinion creating a UT or IT on them is a waste of time. You should cover class's behavior like Db4oProvider<T> with a good Component/"large Integration"(not only the unit + DB) tests. I offer you to read about Test Pyramid(In Martin Fowler blog and this good article). I like the following pyramid:

关于Select方法:

Integration test(单元+ DB)中,您必须将数据放入DB中(或将DB保留有数据...),在有条件的情况下调用Select,然后验证是否已获得期望的数据

In Integration test(unit + DB) you have to put data inside your DB(or keep a DB with data...), call Select with a condition, then verify that you've got the expected data.

Component test中,您需要将其作为被测行为的一部分进行测试,如果测试成功,那么一切都会好起来的...

In Component test you need to test it as a part of behavior under test, if the test succeed then every thing is alright...

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

In UT the tests are split into 2 types:

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

1.The tests where you verify the behaviour of the condition(lambda) you pass into Select method:(in this way you also verify that Select was called with the right parameters... In my examples I use 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以返回一些重要的参数,

2.The tests where you verify caller method behaviour. In those tests you should configure your DAL to return something matter the parameters are:

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

这篇关于DAL和BLL的单元/集成测试(带有lambda)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆