我创建了数据合同,如下所示: -
[DataContract(Name = "Animal")]
public class Animal { ... }
[DataContract(Name = "Dog ")]
public class Dog : Animal { // Has some Dog specific member properties }
[DataContract(Name = "Cat ")]
public class Cat : Animal { // Has some Cat specific member properties }
Run Code Online (Sandbox Code Playgroud)
我正在使用ASP.NET WebApi来提供REST API,如:
[HttpGet]
public Task<Animal> Get(string type)
{
switch(type)
{
case "dog":
Dog dog = GetDogDetails();
return Task.FromResult(dog);
break;
case "cat":
Cat cat = GetCatDetails();
return Task.FromResult(cat);
break:
}
}
Run Code Online (Sandbox Code Playgroud)
我无法归还各自的动物类型.我收到以下错误: - …
使用NSubstitute,如何模拟返回void的方法中抛出的异常?
假设我们的方法签名看起来像这样:
void Add();
Run Code Online (Sandbox Code Playgroud)
以下是NSubstitute文档如何模拟抛出void返回类型的异常.但这不编译:(
myService
.When(x => x.Add(-2, -2))
.Do(x => { throw new Exception(); });
Run Code Online (Sandbox Code Playgroud)
那你怎么做到这一点?
c# ×2
exception ×1
inheritance ×1
nsubstitute ×1
polymorphism ×1
response ×1
unit-testing ×1
void ×1