模拟一个采用默认为null的可选参数的方法

时间:2015-07-13 17:35:52

标签: c# .net elasticsearch moq nest

我正在针对通过NEST点击Elasticsearch的函数编写单元测试。我的单元测试设置如下:

describe('customDirective', function() {
  var element, compile, scope;

  beforeEach(module('example'));

  beforeEach(inject(function($compile, $rootScope) {
    compile = $compile;
    scope = $rootScope.$new();
  }));

    // Manually compile and link our directive
  function getCompiledElement(template) {
    var compiledElement;

    var validTemplate = '<input ng-model="example.data" custom-directive />';

    compiledElement = compile(template || validTemplate)(scope);
    scope.$digest();

    return compiledElement;
  }

  it('should do something', function() {
    element = getCompiledElement();
    // expects
  });

});

var mockResponse = new Mock<IBulkResponse>(); var mockClient = new Mock<IElasticClient>(); mockClient.Setup(x => x.IndexManyAsync<Store>(It.IsAny<IEnumerable<Store>>(), It.IsAny<string>(), It.IsAny<string>())).Returns(Task<IBulkResponse>.Run(() => mockResponse.Object)); 函数的函数签名为IndexManyAsync

正如您所看到的,我尝试将我的模拟Task<IBulkResponse> IndexManyAsync<T>(IEnumerable<T> object, string index = null, string type = null)设置为模拟上面的方法,但我得到以下异常:

IElasticClient

我不清楚这里发生了什么。为什么我无法模拟这个采用可选参数的方法?

1 个答案:

答案 0 :(得分:4)

45 44444 is an extension method

的特殊超载似乎
IndexManyAsync

You cannot use Moq to mock extension methods