简体   繁体   English

使用 Moq 对服务进行单元测试时返回数据 null

[英]Return data null when unit test the service using Moq

I am having a problem when using the Moq for unit testing the a function which accepts number of parameters.使用 Moq 对接受多个参数的函数进行单元测试时遇到问题。 I did the bellow steps to moq the service.我做了以下步骤来最小化服务。

  1. Created a mock object to interface.为接口创建了一个模拟对象。 the relevant method in the interface contains four arguments namely int type, a collection and a object.接口中的相关方法包含四个参数,即 int 类型、一个集合和一个对象。 so i initailized the variable needed for those first,所以我首先初始化了那些所需的变量,

  2. Then setup the mock object to return aa predefined type of object.然后设置模拟对象以返回预定义类型的对象。

  3. After that the relevant method in the service class is called with the required arguments the method does not return the object that it is supposed to return (moq object) but it rather returns a null object.之后,使用所需的参数调用服务类中的相关方法,该方法不会返回它应该返回的对象(moq 对象),而是返回一个空对象。

Am I missing anything in the code bellow?我在下面的代码中遗漏了什么吗?

 Collection<MyDocumentSample spec = new Collection<MyDocumentSample()
 { 
  new MyIdSample() { SampleID = 1234 } };
  int index = 0;
  int maxNoOfRows = 2;
  MyDocumentListSortFields sortFeild = new
  MyDocumentListSortFields() 
  { 
       Descending = false, 
       SortField = MyDocumentListSortFields.SortFields.Date 
  };


var bundleOfObjects = new bundleOfObjects { Entry = new List<bundleOfObjects.EntryComponent() };

MySampleDocument resource_1 = new MySampleDocument();
resource_1.Id = "1005823";
resource_1.Description = "Test 1";
resource_1.Created = "11/20/2017 12:59:47 PM";


bundleOfObjects.AddTobundleOfObjects(resource_1, null);
var m_documentWrapperHelperMock = new Mock<IDocumentSampleHelper();
var m_configMock = new Mock<IConfiguration();

m_documentWrapperHelperMock.Setup(x => x.GetSampleDocumentsWithOffSets(spec, index, maxNoOfRows, sortFeild)).Returns(bundleOfObjects);


var service = new MySampleDocumentService(m_configMock.Object, m_documentWrapperHelperMock.Object);

After that the relevant method in the service class is called with the required arguments the method does not return the object that it is supposed to return (moq object) but it rather returns a null object.之后,使用所需的参数调用服务类中的相关方法,该方法不会返回它应该返回的对象(moq 对象),而是返回一个空对象。

In its default mode Moq will return null when a mock is not invoked like how it was setup, including parameters.在默认模式下,当没有像设置时那样调用模拟时,Moq 将返回null ,包括参数。 Verify that you invoke the mocked member the same way it was setup to be invoked.验证您调用模拟成员的方式与设置为调用的方式相同。

It is most likely that the mocking framework is unable to validate equality of the hard-coded values used to setup the mocked method.模拟框架很可能无法验证用于设置模拟方法的硬编码值的相等性。

To make mocks more flexible with regards to arguments you can use It.IsAny<T>() to let the mock know to accept any instance passed to it.为了使模拟在参数方面更加灵活,您可以使用It.IsAny<T>()让模拟知道接受传递给它的任何实例。

m_documentWrapperHelperMock
    .Setup(_ => _.GetSampleDocumentsWithOffSets(
                     It.IsAny<Collection<MyDocumentSample>>(), 
                     It.IsAny<int>(), 
                     It.IsAny<int>(), 
                     It.IsAny<MyDocumentListSortFields>()
                 )
    ).Returns(bundleOfObjects);

Reference Moq Quickstart参考Moq 快速入门

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

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