NSubstitute.Analyzers检测NSubstitute用法冲突

NSubstitute是一个.Net环境使用的,简洁,语法友好的Mock库。语法简洁的缺点是有一些失败的用法很难察觉和检测。比如试图mock一个非虚拟成员-NSubstitute不能看到这些成员所以不能沟通sub.Received().NonVirtualCall()的问题。多亏了现在有Roslyn编译器。我们现在有了能检测这些问题的方法。

使用用法:

使用Nuget安装NSubstitute.Analyzers.CSharp库。

Install-Package NSubstitute.Analyzers.CSharp

能检测的特性列表如下:

NS1000

在类的非virtual成员上使用

  • Returns()
  • ReturnsForAnyArgs()
  • ReturnsNull()
  • ReturnsNullForAnyArgs()
  • Throws()
  • ThrowsForAnyArgs()等特性

NS1001

在类的非virtual成员上使用

  • Received()
  • ReceivedWithAnyArgs()
  • DidNotReceive()
  • DidNotReceiveWithAnyArgs()

等特性

NS1002

在类的非virtual成员上使用

  • When()
  • WhenForAnyArgs()

等特性

NS2000

使用Substitute.ForPartsOf 创建接口或者代理。 解决方法是使用Substitute.For代替Substitute.ForPartsOf

NS2001

使用NSubstitute mock没有暴露公共或者保护构造方法的类。

NS2002

使用NSubstitute mock没有暴露无参构造方法的类。

NS2003

使用NSubstitute mock 内部类。

NS2004

使用NSubstitute mock的时候传递了错误的构造参数。

NS2005

一次替代多个类型

NS2006

mock接口的时候传递了参数。 只要将参数移除就可以解决这个问题

参考:github目录

原文地址:https://www.cnblogs.com/Martianhh/p/10109311.html