如何为这个业务逻辑代码了坚实的单元测试? [英] How to write a solid unit test for this business logic code?

查看:124
本文介绍了如何为这个业务逻辑代码了坚实的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一些业务逻辑代码,我想测试。
此刻我只知道如何写上还没有其他依赖逻辑代码单元测试。

I got some business logic code I want to test.
At the moment I only know how to write unit test on logic code that hasn't other dependencies.

任何人都可以点我怎么好的方向发展测试例如此功能,也许举个例子?

Can anyone point me in the good direction of how to test for example this function and maybe give an example?

是测试这种集成测试或做我必须使用模拟/存根的唯一途径?

Is the only way to test this an integration test or do I have to use mock/stub?

/// <summary>
/// Gets the scan face file by a MemberID
/// </summary>
/// <param name="MemberID">The ID of a member</param>
/// <returns>A scan face file in byte array format</returns>
public byte[] GetScanFileFaceByMemberID(int MemberID)
{
    byte[] scanFileFace;

    using (ProductionEntities entityContext = new ProductionEntities())
    {
        scanFileFace = (from scan in entityContext.tblScan
                     where scan.MEMBERID == MemberID
                     select scan.scanFileFace).Single();
    }

    return scanFileFace;
}



改变(我实现了资源库和放大器;犀牛嘲笑):

BL:

public byte[] GetScanFileFaceByMemberID(int MemberID)
{
    byte[] scanFileFace;

    var query = Repository.GetAll<tblScan>().Where(bl => bl.MEMBERID == MemberID).Single();

    scanFileFace = query.scanFileFace;

    return scanFileFace;
}



单元测试:

[TestMethod]
public void GetScanFileFace_ExistingScan_ReturnByteArray()
{
    //Make testScan
    List<tblScan> testScan = PrepareTestDataScan();

    //Arrange
    KlantenBL klantenBL = new KlantenBL();

    klantenBL.Repository = MockRepository.GenerateMock<IRepository>();
    klantenBL.Repository.Stub(bl => bl.GetAll<tblScan>()).IgnoreArguments().Return(testScan);

    //Act
    var result = klantenBL.GetScanFileFaceByMemberID(2);

    //assert
    Assert.AreEqual(result.GetType().Name, "Byte[]");
    Assert.AreEqual(result.Length, 10);
}

//Prepare some testData
private List<tblScan> PrepareTestDataScan()
{
    List<tblScan> scans = new List<tblScan>();

    //Declare some variables
    byte[] byteFile = new byte[4];
    byte[] byteFile10 = new byte[10];

    DateTime date = new DateTime(2012,01,01);

    scans.Add(new tblScan { SCANID = 1, MEMBERID = 1, scanFileFace = byteFile, Hair = byteFile, scanFileAvatar = byteFile, scanFileMeasurements = byteFile, scanDate = date });
    scans.Add(new tblScan { SCANID = 2, MEMBERID = 2, scanFileFace = byteFile10, Hair = byteFile, scanFileAvatar = byteFile, scanFileMeasurements = byteFile, scanDate = date });
    scans.Add(new tblScan { SCANID = 3, MEMBERID = 3, scanFileFace = byteFile, Hair = byteFile, scanFileAvatar = byteFile, scanFileMeasurements = byteFile, scanDate = date });

    return scans;
}



存储库:

public IList<T> GetAll<T>()
{
    DZine_IStyling_ProductionEntities context = GetObjectContext();
    IList<T> list = context
        .CreateQuery<T>(
        "[" + typeof(T).Name + "]")
        .ToList();
    ReleaseObjectContextIfNotReused();
    return list;
}

public IList<T> GetAll<T>(Func<T, bool> expression)
{
    DZine_IStyling_ProductionEntities context = GetObjectContext();
    IList<T> list = context
        .CreateQuery<T>(
        "[" + typeof(T).Name + "]")
        .Where(expression)
        .ToList();
    ReleaseObjectContextIfNotReused();
    return list;
}



这个工作非常感谢大家!

推荐答案

从我的角度来看...

From my perspective...

您逻辑是不能测试如果通过直接DataBaseEntities上下文(ProductionEntities)与数据库进行交互。因为你的逻辑依赖于ProductionEntities。

Your logic is not be able to test if you interact with database via DataBaseEntities context(ProductionEntities) directly. because your logic that depend on ProductionEntities.

我建议你遵循的库格式为您的数据访问层。你将能够使你的代码是可测试性的逻辑。该模式将帮助您从注入逻辑数据访问层。

I would recommend you to follow Repository Pattern for your data access layer. You would be able to make you code to be testability logic. The pattern will help you to inject data access layer from logic.

我也想建议你遵循依赖注入模式。这种模式可以帮助你做单元测试代码更容易。你将能够使用模拟框架像犀牛模拟,以帮助您的单元测试。

I also would like to recommend you to follow dependency injection pattern. This pattern will help you to do unit test your code easier. You will be able to use mock framework like Rhino mock to help you on unit testing.

这篇关于如何为这个业务逻辑代码了坚实的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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