如何使用Mock在C#中集合测试一个帮助函数调用

时间:2017-08-14 18:02:32

标签: c# moq

我是单元测试的新手,我想为以下代码创建一个单元测试:

   public IEnumerable<Product> CreateProductList(
       IEnumerable<int> productsIds, IEnumerable<Product> products)
    {
        var productsList = productsIds.GroupJoin(
            inner: products, 
            outerKeySelector: x => x, 
            innerKeySelector: y => y.Id,
            resultSelector: (x, y) => _productServiceHelper.
            FactoryProduct(x, y.FirstOrDefault(k => k.Id == x))).ToList();

        return productsList;
    }

我到目前为止编写的单元测试试图使用MockSequence但是没有成功:

   [Test, TestCaseSource(nameof(TestCases_CreateProductList_SomeMissingIds))]
    public void CreateProductList_SomeMissingIds_ReturnListContainingMissingOnes
        (IEnumerable<int> listWithRequiredProductsIds, IEnumerable<Product> listMissingIds,
        IEnumerable<Product> expectedListContainingAllRequired)
    {
        //_mockProductServiceHelper.InSequence(sequence).Setup(x => 
        //    x.FactoryProduct(It.IsAny<int>(), It.IsAny<Product>())).Returns(()=> expectedListContainingAllRequire(x => x));

        //expectedListContainingAllRequired.ToList()
           // .Select(x => _mockProductServiceHelper.InSequence(sequence)
                       // .Setup(y => y.FactoryProduct(It.IsAny<int>(), It.IsAny<Product>()))
                    //.Returns(x));
        //expectedListContainingAllRequired.
        var result = _productService.CreateProductList(listWithRequiredProductsIds,
                        listMissingIds);


        var jsonResult = JsonConvert.SerializeObject(result);
        var jsonExpectedListContainingAllRequired = JsonConvert.SerializeObject
                                        (expectedListContainingAllRequired);
        //_mockProductServiceHelper.Verify(x => x.FactoryProduct(),Times.Once);

        //Assert.That(result.Count(), Is.EqualTo(expectedListContainingAllRequired.Count()));
        Assert.That(result.Count(), Is.EqualTo(expectedListContainingAllRequired.Count()));
        Assert.That(jsonResult, Is.EqualTo(jsonExpectedListContainingAllRequired));
    }

    public static IEnumerable TestCases_CreateProductList_SomeMissingIds
    {
        get
        {
            yield return DataCases_CreateProductList_SomeMissingIds(
                new List<int> { 1, 2 }, new List<int> { 1 });
            yield return DataCases_CreateProductList_SomeMissingIds(
                new List<int> { 1, 2, 3, 4 }, new List<int> { 1, 2 });
        }
    }

    private static TestCaseData DataCases_CreateProductList_SomeMissingIds
        (IEnumerable<int> productIDs, IEnumerable<int> productObjects)
    {
        var productListObjects = productObjects.Select(CreateMockObject);

        var expectedProductList = productIDs.Select(CreateMockObject);

    return new TestCaseData(productIDs, productListObjects, expectedProductList);
    }

    private static Product CreateMockObject(int x)
    {
        return new Product
        {
            Category = new Category { Id = 1, Name = "padrao" },
            Name = "Product " + x,
            Description = "Any Description " + x,
            Creation = DateTime.Now.Date,
            Launch = DateTime.Now.Date,
            Price = x * 2
    };
    }

我尝试了很多东西,但显然没有任何作用。我留下了一些我试图遵循的路径。

是否有人有建议或示例有助于完成我的方法测试?

0 个答案:

没有答案