首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >犀牛毛需要帮助

犀牛毛需要帮助
EN

Stack Overflow用户
提问于 2015-08-18 10:17:04
回答 1查看 113关注 0票数 1

enter code here我有下面的方法,我需要编写单元测试用例来测试这个方法。

代码语言:javascript
复制
    public void ProcessDormantApplications()
    {
        List<long> dormantApplicationIDs = new List<long>();

        dormantApplicationIDs = service.GetDormantApplications();

        if (dormantApplicationIDs.Count > 0)
        {

            foreach (long dormantID in dormantApplicationIDs)
            {
                string msg = service.UpdateDormantApplications(dormantID);
             }
            }

           }
       }

这就是我写的测试方法。

代码语言:javascript
复制
[TestClass]
public class DormantApplicationsTest
{

    ILogger logger;
    IService Service;

    [TestInitialize]
    public void SetUp()
    {
        logger = MockRepository.GenerateStub<ILogger>();
        Service = MockRepository.GenerateStub<IService>();
    }

    [TestMethod]
    public void TESTProcessDormantApplications()
    {
        ////arrange
        ////act
        var target = new BLogic(service, logger);
        target.ProcessDormantApplications();

        ////assert
       // service.AssertWasCalled(x => x.
    }
}

实际方法调用另一个服务层,该层调用web服务来获取数据。在这个场景中,我不知道在这种情况下ASSERT应该做什么。

代码语言:javascript
复制
[TestMethod]
    public void CheckProcessDormantApplications_InBetweenApplicationFailedToUpdate()
    {
        ////arrange
        var applicationIds = new List<long>()
        {
            1,2,3

        };
        UpdateResponse.isSuccess = true;
        UpdateResponse.errorMessage = string.Empty;

        Service.Stub(x => x.GetDormantApplications()).Return(applicationIds);
        for(int i=0; i <= applicationIds.Count-1; i++)
        {
            if (i == 1) //set this application id response to FALSE so it should continnue with next record as well
            {
                UpdateResponse.isSuccess = false;
                UpdateResponse.errorMessage = "making it fail for test";
            }

            Service.Stub(x =>x.UpdateDormantApplications(applicationIds[i])).Return(UpdateResponse);
        }
        ////act
        var target = new BLogic(Service, logger);
        target.ProcessDormantApplications();

        ////assert
        foreach (long id in applicationIds)
        {
            Service.AssertWasCalled(x => x.UpdateDormantApplications(id));
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-18 10:59:12

根据您提供的代码,您必须在GetDormantApplications上设置一个返回某些id的行为,然后验证是否为每个id调用了UpdateDormantApplications

代码语言:javascript
复制
[TestMethod]
public void Check_ProcessDormantApplications()
{
    ////arrange
    var applicationId = new List<long>()
    {
        //put here some ids
    };

    DormantServiceAdapter.Stub(x => x.GetDormantApplications()).Return(applicationId);
    var target = new DormantApplicationsBusinessLogic(DormantServiceAdapter, logger);

    ////act        
    target.ProcessDormantApplications();

    ////assert
    foreach (var id in applicationId)
    {
        DormantServiceAdapter.AssertWasCalled(x => x.UpdateDormantApplications(id));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32069624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档