Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编写moq测试以设置返回计数值的方法_C#_Asp.net Mvc_Unit Testing_Moq - Fatal编程技术网

C# 如何编写moq测试以设置返回计数值的方法

C# 如何编写moq测试以设置返回计数值的方法,c#,asp.net-mvc,unit-testing,moq,C#,Asp.net Mvc,Unit Testing,Moq,在我的控制器操作中,每当添加新产品时,我都会在数据库中检查此产品编号是否已不存在。此检查的代码如下所示 public ActionResult Index(ProductModel model) { var productCount = _productsService.GetAll(true).Count(x => x.ProductNumber == model.ProductNumber); if (productCount > 0)

在我的控制器操作中,每当添加新产品时,我都会在数据库中检查此产品编号是否已不存在。此检查的代码如下所示

 public ActionResult Index(ProductModel model)
    {
      var productCount = _productsService.GetAll(true).Count(x => x.ProductNumber == model.ProductNumber);

      if (productCount > 0)
          ModelState.AddModelError("ProductNumber", Product already present in the system!");

       // more processing
     }
我不熟悉MOQ测试,并尝试编写一个单元测试来设置GetAll方法,该方法将返回0。我写过这样的东西,但似乎不起作用

 var _productsService = new Mock<IProductsService>();
_productsService.Setup(m => m.GetAll(true).Count()).Returns(0);
var_productsService=new Mock();
_productsService.Setup(m=>m.GetAll(true.Count())。返回(0);
有什么想法吗?谢谢

看看这篇文章:


他基本上也有同样的问题,您试图模拟一个扩展方法
Count()

这不是您使用Moq的方式--
Count
很可能不是您的方法(它是LINQ/其他第三方),您不会模拟它。您需要模拟的是
GetAll
method,它是可模拟依赖项上的一个方法。您可以“告诉”
GetAll
返回模型与参数匹配的产品,如下所示:

[Test]
public void Index_ReportsModelError_WhenProductAlreadyExists()
{
    const int ExistingProductNumber = 10;
    var _productsService = new Mock<IProductsService>();
    var existingProduct = new Product { ProductNumber = ExistingProductNumber };
    _productsService.Setup(m => m.GetAll(true)).Returns(new [] { existingProduct });

    controller.Index(new ProductModel { ProductNumber = ExistingProductNumber });

    // Assert
}

您想要测试的功能到底是什么?控制器方法或_productsService.GetAll()?@greenhoorn我想测试控制器操作,但在该操作中,我调用GetAll方法,需要设置该方法并获取一个值。是否收到错误?System.NotSupportedException:表达式引用不属于模拟对象的方法:m=>m.GetAll(True).Count()“GetAll”方法看起来怎么样?我正在编写其他测试代码。我不得不在控制器操作中添加此检查(即,产品编号不存在),现在这些单元测试由于此更改而失败。我现在正在尝试解决如何通过“这个GetAll检查的添加和在ModelState中添加这个错误”。我想设置一些东西,比如,在添加此检查之前编写的单元测试中,此代码总是通过旁路。是的,我当然可以添加一个新的单元测试来检查存储库中的GetAll方法。@学习者:“是的,我当然可以添加一个新的单元测试来检查存储库中的GetAll方法”--为什么要这样做?您可以测试控制器,而不是
GetAll
GetAll
是控制器的依赖项,您可以将其存根(就像现在一样)。您提到您添加了破坏测试的新功能。这需要更改测试--您需要确保
GetAll
的现有设置不会返回与传递给
Index
方法的产品编号相同的产品。感谢@jimmy_keen您澄清了我的概念。有什么好的在线教程、关于使用存储库进行Moq测试的书籍或视频的建议吗?@Learner:为了与Moq相处,我建议他们的wiki页面--。这个特殊的案例(存储库测试)不需要任何特别的教程。。。存储库只是您模拟的依赖项。你最好找一些关于ASP.NET MVC控制器单元测试的教程,网上有很多。
const int ExistingProductNumber = 10;
const int NewProductNumber = 20;
var _productsService = new Mock<IProductsService>();
var existingProduct = new Product { ProductNumber = ExistingProductNumber };
_productsService.Setup(m => m.GetAll(true)).Returns(new [] { existingProduct });

controller.Index(new ProductModel { ProductNumber = NewProductNumber });

// Assert