【问题标题】:NUnit generated Mock Repository returns the same object in different test casesNUnit 生成的 Mock Repository 在不同的测试用例中返回相同的对象
【发布时间】:2017-03-10 01:49:27
【问题描述】:

您好,我刚开始使用测试驱动开发。我有一个代码,其中有两个测试用例

     [Test, Order(3)]
    public void Should_Not_Create_ServiceAccountTaxCode_If_BillType_Is_Not_RateReady()
    {
        //ARRANGE
        var customerDetailsViewForBillTYpeRateReady = new CustomerTaxDetailsView
        {
            BillType = (int)BillTypes.BillReady
        };

        _repository.Stub(x => x.GetCustomerDetailsForTaxes(Arg<int>.Is.Anything)).Return(dict.Dequeue());


        //ACT
        var result = _concern.PopulateServiceAccountWithTaxDetails(Arg<int>.Is.Anything);

    [Test, Order(4)]
    public void Should_Create_ServiceAccountTaxCode_If_BillType_Is_RateReady()
    {
        //ARRANGE
        const int serviceAccountId = 1;
        var customerDetailsView = new CustomerTaxDetailsView
        {
            BillType = (int)BillTypes.RateReady,
            ServiceTypeId = (int)ServiceTypes.Electric
        };
       _repository.Stub(x => x.GetCustomerDetailsForTaxes(serviceAccountId))
            .Return(customerDetailsView).Repeat.Once();
        var result = _concern.PopulateServiceAccountWithTaxDetails(serviceAccountId);

我在

中使用以下语法生成模拟
    [OneTimeSetUp]
    public void Initialize()
    {
     _repository = MockRepository.GenerateMock<IServiceAccountTaxCodeRepository>();

唯一的问题是,在第二个测试用例中,我的结果对象也是来自第一个测试用例的 customerDetailsViewForBillTYpeRateReady。为什么会发生这种情况。如果我独立运行这些测试,那么一切都会通过。任何帮助将不胜感激..

【问题讨论】:

  • dict.dequeue 实际上将 customerDetailsViewForBillTYpeRateReady 对象出列。

标签: c# asp.net nunit moq rhino-mocks


【解决方案1】:

我相信您的问题是因为您在同一个 _repository 对象的两个位置配置了方法 GetCustomerDetailsForTaxes()。此配置将始终执行:

_repository.Stub(x => x.GetCustomerDetailsForTaxes(Arg<int>.Is.Anything)).Return(dict.Dequeue());

原因是因为您指定了Arg&lt;int&gt;.Is.Anything。因此,测试用例将获取该配置,因为在第二个测试用例中您有 const int serviceAccountId = 1;,这也是 Arg&lt;int&gt;.Is.Anything

我宁愿在第一个测试用例中指定

 const int serviceAccountId = 2;
_repository.Stub(x => x.GetCustomerDetailsForTaxes(serviceAccountId)).Return(dict.Dequeue());

现在您将有两个测试用例的不同配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 2014-11-30
    • 2012-02-10
    • 2015-08-05
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多