需要Rhino mock的帮助

本文关键字:帮助 mock Rhino 需要 | 更新日期: 2023-09-27 18:08:24

enter code here我有以下方法,我需要编写单元测试用例来测试此方法。

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

,这是我写的TEST方法。

[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是什么。

[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));
        }
    }
}

需要Rhino mock的帮助

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

[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));
    }
}