简体   繁体   English

使用最小起订量的扩展方法的单元测试

[英]Unit Testing for Extension method using moq

I'm writing a unit test for function uploaddocuments() for azuresearch.我正在为 azuresearch 编写 function uploaddocuments() 的单元测试。

 Unsupported expression: ... =>....Index(It.IsAny<IndexBatch<Document>>(), It.IsAny<SearchRequestOptions>()) Extension methods (here: DocumentsOperationsExtensions.Index) may not be used in setup / verification expressions.

Not sure why it's not working.不知道为什么它不起作用。

code:代码:

    private static async Task uploaddocuments(ISearchIndexClient indexClient)
    {

    var actions = new IndexAction<Hotel>[]
    {

      IndexAction.Upload(
       new Hotel()
       {
           HotelId = "1",
                HotelName = "Secret Point Motel",
                Description = "The hotel is ,

                Rating = 3.6,
                Address = new Address()
                {
                    StreetAddress = "677 5th Ave",
                    City = "New York",
                    StateProvince = "NY",
                    PostalCode = "10022",
                    Country = "USA"
                }

         }
       )
    }
    var batch = IndexBatch.New(actions);
    try
    {
           indexClient.Documents.Index(batch);
    }
    catch (IndexBatchException e)
    {
          console.log(e);
    }
}

Test:测试:

var testMock = new Mock(IDocumentsOperations)();

docOperationsMock.Setup(() => Index(It.IsAny(IndexBatch<Document))(), It.IsAny<SearchRequestOptions)())).Returns(new DocumentIndexResult());


  var mock = new Mock<ISearchIndexClient>()

            .Setup(x => x).Returns(It.IsAny(SearchIndexClient)());

             .SetupGet(a => a.Documents).Returns(It.IsAny("IDocumentsOperations")())

             .Callback(() => IndexBatch.Upload(It.IsAny(IEnumerable(dynamic))()));

            .Returns(testMock.Object);

You can't directly mock static method (eg extension method) with mocking framework.You can use some wrapper to achieve the same.您不能使用 mocking 框架直接模拟 static 方法(例如扩展方法)。您可以使用一些包装器来实现相同的目的。 We can not (by default) mock the static call – it's a tight coupling that can not be easily broken.我们不能(默认情况下)模拟 static 调用——这是一个不能轻易破坏的紧密耦合。

Here is a very nice article which shows a way to create a wrapper for static method which can help us in this scenario:这是一篇非常好的文章,它展示了一种为 static 方法创建包装器的方法,它可以在这种情况下帮助我们:

http://adventuresdotnet.blogspot.com/2011/03/mocking-static-methods-for-unit-testing.html http://adventuresdotnet.blogspot.com/2011/03/mocking-static-methods-for-unit-testing.html

Alternatively you can use PEX or MOLES for achieving the same result, you can read it further in below doc:或者,您可以使用 PEX 或 MOLES 来获得相同的结果,您可以在下面的文档中进一步阅读:

https://www.microsoft.com/en-us/research/project/pex-and-moles-isolation-and-white-box-unit-testing-for-net/?from=http%3A%2F%2Fresearch.microsoft.com%2Fen-us%2Fprojects%2Fpex%2Fdownloads.aspx https://www.microsoft.com/en-us/research/project/pex-and-moles-isolation-and-white-box-unit-testing-for-net/?from=http%3A%2F%2Fresearch .microsoft.com%2Fen-us%2Fprojects%2Fpex%2Fdownloads.aspx

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM