简体   繁体   English

如果使用NSubstitute出现问题,则模拟一个抛出异常的方法

[英]Mocking a method that throws exception if something went wrong using NSubstitute

I have implemented a service in my .NET Core application, that I call to validate my model. 我已经在.NET Core应用程序中实现了一项服务,调用该服务来验证我的模型。 Unfortunately, the service (not mine) throws an exception if it's invalid and simply responds with a 200 OK if it's valid (since it's a void method). 不幸的是,服务(不是我的)如果无效则抛出异常,如果有效则简单地以200 OK响应(因为它是void方法)。

So basically I do something like this: 所以基本上我做这样的事情:

try {
    await _service.ValidateAsync(model);
    return true;
} catch(Exception e) {
    return false;
}

I am trying to mock the method inside of ValidateAsync , that sends the request to the service I implemented. 我试图模拟ValidateAsync内的方法,该方法将请求发送到我实现的服务。 ValidateAsync only converts my controller's input from something on my frontend, to something the Validate method understands. ValidateAsync仅将控制器的输入从前端的某些内容转换为Validate方法可以理解的内容。

However, I am unable to really see how I am supposed to test this. 但是,我无法真正看到应如何测试。 Here's what I have tried, but it doesn't really make any sense to me. 这是我尝试过的方法,但对我来说真的没有任何意义。

[TestMethod]
public void InvalidTest() {
    var model = new Model(); // obviously filled out with what my method accepts

    _theService.Validate(model)
        .When(x => { }) //when anything
        .Do(throw new Exception("didn't work")); //throw an exception

    //Assert should go here.. but what should it validate?
}

So basically: When this is called -> throw an exception . 基本上When this is called -> throw an exception这样: When this is called -> throw an exception

How am I supposed to mock that using NSubstitute? 我应该如何使用NSubstitute模拟它?

Based on current explanation the assumption is something like 根据目前的解释,假设类似于

public class mySubjectClass {

    private ISomeService service;

    public mySubjectClass(ISomeService service) {
        this.service = service;
    }

    public async Task<bool> SomeMethod(Model model) {
        try {
            await service.ValidateAsync(model);
            return true;
        } catch(Exception e) {
            return false;
        }
    }

}

In order to cover a false path for SomeMethod , the dependency needs to throw an exception when invoked so that the test can be exercised as expected. 为了覆盖SomeMethod的错误路径,依赖项在调用时需要引发异常,以便可以按预期执行测试。

[TestMethod]
public async Task SomeMethod_Should_Return_False_For_Invalid_Model() {
    //Arrange
    var model = new Model() { 
        // obviously filled out with what my method accepts
    };

    var theService = Substitute.For<ISomeService>();
    theService
        .When(_ => _.ValidateAsync(Arg.Any<Model>()))
        .Throw(new Exception("didn't work"));

    var subject = new mySubjectClass(theService);

    var expected = false;

    //Act
    var actual = await subject.SomeMethod(model);

    //Assert
    Assert.AreEqual(expected, actual);
}

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

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