简体   繁体   English

为什么该方法不返回自定义异常消息

[英]Why the method does not return custom exception message

I would like to an async method "UpdateAsync" return custom exception message when PutAsync method is invoked. 我希望异步方法“ UpdateAsync”在调用PutAsync方法时返回自定义异常消息。 What I do now is mock the class which is PutAsync belong to, and then I setup the method and give the parameter. 我现在要做的是模拟PutAsync所属的类,然后设置方法并提供参数。 I also use Throws to custom exception message. 我还使用Throws自定义异常消息。

The problem is when I run this 问题是当我运行这个

var result = await this.repository.UpdateAsync(new EndPoint(new Uri(testUrl), HttpMethod.Put), JObject.FromObject(new object()), this.exceptionProcessor);

The PutAsync keep running without return exception. PutAsync继续运行,没有返回异常。 Here is the code. 这是代码。

Mock<RestClient> rc = new Mock<RestClient>();
rc.Setup(x => x.PutAsync(new Uri(testUrl), JObject.FromObject(new object()), new NameValueCollection()))
.Throws(new Exception("TestMessage"));

var result = await this.repository.UpdateAsync(new EndPoint(new Uri(testUrl), HttpMethod.Put), JObject.FromObject(new object()), this.exceptionProcessor);
Assert.IsTrue(result.ErrorMessages.GetValue(string.Empty).Equals("TestMessage"));

here is the main part of UpdateAsync, when process goes here, it will enter GetClient() first and then jump to Exception direct. 这是UpdateAsync的主要部分,当流程进行到此处时,它将首先输入GetClient(),然后直接跳转到Exception。 This test was wrote using Shimes, but we don't want to use Shimes anymore, therefore I need to use another way to do. 该测试是使用Shimes编写的,但是我们不再希望使用Shimes,因此我需要使用另一种方法。

public virtual async Task<GenericOperationResult<object>> UpdateAsync(EndPoint endpoint, JContainer obj, IExceptionProcessor exceptionProcessor, NameValueCollection headers){

     if (endpoint.ActionMethod == HttpMethod.Put)
     {
        result = await this.GetClient().PutAsync(endpoint.Url, obj, headers);
     }
     else if (endpoint.ActionMethod == HttpMethod.Post)
     {
        result = await this.GetClient().PostAsync(endpoint.Url, obj, headers);
     }
     else
     {
        throw new ConfigurationException("Update supports only POST or PUT verbs. Check endpoint configuration.");
     }
     return new GenericOperationResult<object>(200, result); 
}

You are instantiating new objects in your setup, which are different from the objects you are instantiating in your call to UpdateAsync, so they won't match and the Mock object won't throw the exception. 您正在实例化设置中的新对象,这些对象不同于在调用UpdateAsync时实例化的对象,因此它们将不匹配,并且Mock对象不会引发异常。 You could instead setup the mock to throw the exception if objects of the correct types are passed in, with the Url param also checking it has the testUrl, for example: 如果传递了正确类型的对象,则可以改为设置模拟以引发异常,Url参数还检查其是否具有testUrl,例如:

rc.Setup(x => x.PutAsync(It.Is<Uri>(u => u.OriginalString == testUrl), It.IsAny<JObject>(), It.IsAny<NameValueCollection>())
    .ThrowsAsync(new Exception("TestMessage"));

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

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