I would like to an async method "UpdateAsync" return custom exception message when PutAsync method is invoked. What I do now is mock the class which is PutAsync belong to, and then I setup the method and give the parameter. I also use Throws to custom exception message.
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. 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. This test was wrote using Shimes, but we don't want to use Shimes anymore, therefore I need to use another way to do.
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);
}