错误“使用moq进行单元测试时,模拟一次的预期调用,但是为0次”

时间:2016-01-26 15:54:24

标签: unit-testing moq mstest

我有以下要测试的课程:

KeyEvent.VK_DOWN/VK_UP

具体来说,我想测试GetData方法。我的单元测试看起来像这样:

public interface ISqlServiceByModule
{
    DataSet GetPagedAggregateData(int clientId, int moduleId, int billTypeId, PagedTable result);
}


public class IncidentModuleService : IIncidentModuleService
{
    private readonly ISqlServiceByModule sqlServiceByModule;

    public IncidentModuleService(ISqlServiceByModule sqlServiceByModule)
    {
        if (sqlServiceByModule == null) throw new ArgumentNullException("sqlServiceByModule");

        // Inject the ISqlServiceByModule dependency into the constructor
        this.sqlServiceByModule = sqlServiceByModule;
    }

    public PagedTable GetData(int clientId, int moduleId, int billTypeId, Dictionary<string, string> queryStringParameters)
    {            
        PagedTable result = new PagedTable(queryStringParameters);

        DataSet dataSet = this.sqlServiceByModule.GetPagedAggregateData(clientId, moduleId, billTypeId, result);

        // Map the DatSet to a PagedTable
        if (dataSet == null || dataSet.Tables.Count == 0)
        {
            result.SetPagesFromTotalItems(0);
        }
        else
        {
            result.SetPagesFromTotalItems(Convert.ToInt16(dataSet.Tables[1].Rows[0][0]));
            result.Listings = dataSet.Tables[0];
        }

        return result;
    }
}

我得到的错误发生在最后一行:

[TestClass]
public class IncidentModuleServiceUnitTest
{
    private DataSet incidentsData;

    [TestInitialize]
    public void SetUp()
    {
        this.incidentsData = new DataSet();
    }

    [TestMethod]
    public void GetDataTestGetPagedAggregateDataIsCalled()
    {
        //-- Arrange
        int billTypeId = 1;
        int clientId = 1;
        int moduleId = 1;
        Dictionary<string, string> queryStringParameters = new Dictionary<string,string>();
        PagedTable tempResult = new PagedTable(queryStringParameters);

        DataSet dataSet = new DataSet();
        dataSet.Tables.Add(new DataTable());

        var mockSqlService = new Mock<ISqlServiceByModule>();
        mockSqlService.Setup(r => r.GetPagedAggregateData(clientId, moduleId, billTypeId, tempResult)).Returns(this.incidentsData);
        IncidentModuleService target = new IncidentModuleService(mockSqlService.Object);

        //-- Act
        var actual = target.GetData(clientId, moduleId, billTypeId, queryStringParameters);

        //-- Assert
        Assert.IsNull(actual.Listings);
        mockSqlService.Verify(r => r.GetPagedAggregateData(clientId, moduleId, billTypeId, tempResult), Times.Once);
    }
}

确切的错误信息是:

  

{“\ r \ n对模拟的预期调用一次,但是是0次:r =&gt;   r.GetPagedAggregateData(.clientId,.moduleId,.billTypeId,.tempResult

     

已配置的设置:\ r \ nr =&gt; r.GetPagedAggregateData(.clientId,   .moduleId,.billTypeId,.tempResult),   Times.Never

     

执行调用:\ r \ nISqlServiceByModule.GetPagedAggregateData(1,   1,1,PagedTable)“}

知道为什么会这样吗?在我看来,正在调用所讨论的方法,但是由于某种原因,Moq不喜欢这些参数,即使它们在所有三个调用中都是完全相同的,据我所知。

1 个答案:

答案 0 :(得分:3)

PagedTable是引用类型而不是值类型。因此,Setup中的参数与所调用的参数不匹配,即使它们看起来应该相同。您可以使用It.IsAny<PagedTable>()代替tempResult。

有关如何检查PagedTable参数是否正确的示例,请参阅this answer