通过使用 ReturnsForAnyArgs() 方法,可以设置当一个方法被调用后,无论参数是什么,都返回指定的值。

 1     public interface ICalculator
 2     {
 3       int Add(int a, int b);
 4       string Mode { get; set; }
 5     }
 6 
 7     [TestMethod]
 8     public void Test_ReturnForAnyArgs_ReturnForAnyArgs()
 9     {
10       var calculator = Substitute.For<ICalculator>();
11 
12       calculator.Add(1, 2).ReturnsForAnyArgs(100);
13       Assert.AreEqual(calculator.Add(1, 2), 100);
14       Assert.AreEqual(calculator.Add(-7, 15), 100);
15     }

同样的行为也可以通过参数匹配器来达成:可简单快捷地通过 Arg.Any<T>() 来替换每个参数。

ReturnsForAnyArgs() 具有与 Returns() 方法相同的重载,所以也可以指定多个返回值,或者计算返回值。

NSubstitute 完全手册

相关文章:

  • 2021-05-16
  • 2021-05-19
  • 2021-09-22
  • 2021-09-08
  • 2021-05-21
  • 2021-09-02
  • 2021-06-21
猜你喜欢
  • 2021-10-17
  • 2021-11-09
  • 2021-10-14
  • 2021-12-16
  • 2021-11-04
  • 2021-06-26
  • 2021-06-23
相关资源
相似解决方案