Expression references a method that does not belong to the mocked object

后端 未结 4 1532
耶瑟儿~
耶瑟儿~ 2020-12-05 04:44

I have an api service that calls another api service. When I set up the Mock objects, it failed with an error:

NotSupportedException: expression refer

4条回答
  •  天涯浪人
    2020-12-05 05:14

    You have:

    _mockCarrierService = new Mock>>();
    

    So you mock IEnumerable<>. The only member IEnumerable<> has is a method GetEnumerator() (plus another method with the same signature GetEnumerator() inherited from the base interface). The Select method is really an extension method (as was pointed out in the first answer) which is a static method that works by calling GetEnumerator() (possibly through C# foreach statement).

    It is possible to make things work by doing Setup of GetEnumerator on your mock.

    However, it is much simpler to simply use a concrete, non-mock type which "is" IEnumerable<>, such as List<>. So try:

    _mockCarrierService = new List>();
    

    Then add an entry to the List<>. What you should add, is a Mock> on which GetFromApiWithQuery Method is setup.

提交回复
热议问题