最小起订量和设置不返回值

时间:2018-09-18 09:49:33

标签: c# unit-testing nunit moq

我有这个测试:

[Test]
public async Task ShouldSortOnAllFormulas()
{

    // Assemble
    const string categoryId = "cameras";
    const string organisationId = "piiick";
    var services = PickContext.GivenServices();
    var pickProvider = services.WhenCreatePickProvider();
    var products = new List<JObject>
    {
        JObject.Parse("{ \"gtin\": \"1\", \"action\": \"No\", \"size\": \"Large\", \"variant\": \"1\" }"),
        JObject.Parse("{ \"gtin\": \"2\", \"action\": \"No\", \"size\": \"Small\", \"variant\": \"1\" }"),
        JObject.Parse("{ \"gtin\": \"3\", \"action\": \"No\", \"size\": \"Small (No Lens)\", \"variant\": \"1\" }")
    };
    var formulas = new List<AnswerFormula>
    {
        new AnswerFormula { Expression = "No", Field = "action", Operator = "=", AnswerId = 170, Filter = true },
        new AnswerFormula { Expression = "Small", Field = "size", Operator = "%", AnswerId = 170, Filter = true },
        new AnswerFormula { Expression = "Small", Field = "size", Operator = "%", AnswerId = 171 }
    };

    services.MockProductProvider.Setup(x => x.ListAvailableMasterProductsAsync(categoryId, organisationId)).ReturnsAsync(products);
    services.MockFilterProvider.Setup(x => x.Filter(products, formulas)).Returns(products);

    // Act
    var uniqueAnswerIds = formulas.Select(m => m.AnswerId).GroupBy(m => m).Select(m => m.First()).ToList();
    await pickProvider.ProcessProductsAsync(formulas, categoryId, organisationId);

    // Assert
    services.MockSortProvider.Verify(x => x.SortAsync(categoryId, products, uniqueAnswerIds));
}

该测试失败,但是失败,因为产品为空。 由于某种原因,该设置未返回产品列表。

实际上,这些方法会返回新的产品列表,但我希望这样的设置会很好。 我已将设置更改为此:

services.MockProductProvider.Setup(x => x.ListAvailableMasterProductsAsync(categoryId, organisationId)).ReturnsAsync(products.ToList());
services.MockFilterProvider.Setup(x => x.Filter(products, formulas)).Returns(products.ToList());

因此,他们正在为每种设置创建一个新的产品列表,但是在我的方法中,产品列表为null。 有人知道为什么吗?

这是我正在测试的方法:

public async Task<IEnumerable<JObject>> ProcessProductsAsync(IEnumerable<AnswerFormula> formulas, string categoryId, string oranisationId)
{
    if (formulas == null) throw new ArgumentNullException(nameof(formulas));

    var listOfFormulas = formulas.Where(m => m.Filter).ToList();
    var answerIds = formulas.Select(m => m.AnswerId).GroupBy(m => m).Select(m => m.First()).ToList(); // Use all formulas, not just filtered
    var products = await _productProvider.ListAvailableMasterProductsAsync(categoryId, oranisationId);

    _scoreProvider.Value.Score(products, listOfFormulas); // Score
    var filteredProducts = _filterProvider.Filter(products, listOfFormulas); // Filter
    var sortedProducts = await _sortProvider.Value.SortAsync(categoryId, filteredProducts, answerIds); // Sort
    _filterProvider.RemoveVariants(sortedProducts); // Remove duplicates

    return sortedProducts;
}

1 个答案:

答案 0 :(得分:1)

我尝试了多种变体,并不是导致问题的产品。这是公式。不知道为什么,但是将我的测试更改为:

[Test]
public async Task ShouldSortOnAllFormulas()
{

    // Assemble
    const string categoryId = "cameras";
    const string organisationId = "piiick";
    var services = PickContext.GivenServices();
    var pickProvider = services.WhenCreatePickProvider();
    var products = new List<JObject>
    {
        JObject.Parse("{ \"gtin\": \"1\", \"action\": \"No\", \"size\": \"Large\", \"variant\": \"1\" }"),
        JObject.Parse("{ \"gtin\": \"2\", \"action\": \"No\", \"size\": \"Small\", \"variant\": \"1\" }"),
        JObject.Parse("{ \"gtin\": \"3\", \"action\": \"No\", \"size\": \"Small (No Lens)\", \"variant\": \"1\" }")
    };
    var formulas = new List<AnswerFormula>
    {
        new AnswerFormula { Expression = "No", Field = "action", Operator = "=", AnswerId = 170, Filter = true },
        new AnswerFormula { Expression = "Small", Field = "size", Operator = "%", AnswerId = 170, Filter = true },
        new AnswerFormula { Expression = "Small", Field = "size", Operator = "%", AnswerId = 171 }
    };

    services.MockProductProvider.Setup(x => x.ListAvailableMasterProductsAsync(categoryId, organisationId)).ReturnsAsync(products);
    services.MockFilterProvider.Setup(x => x.Filter(products, It.IsAny<List<AnswerFormula>>())).Returns(products);

    // Act
    var uniqueAnswerIds = formulas.Select(m => m.AnswerId).GroupBy(m => m).Select(m => m.First()).ToList();
    await pickProvider.ProcessProductsAsync(formulas, categoryId, organisationId);

    // Assert
    services.MockSortProvider.Verify(x => x.SortAsync(categoryId, products, uniqueAnswerIds));
}

很好。