Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How do I mock the DocumentClientException that the Azure DocumentDB client library throws?

$
0
0

Hi,

 We are porting an existing .Net 452 application which uses the Microsoft.Azure.DocumentDB package to ASP .Net Core.

 In the existing version we were using the approach mentioned @ http://stackoverflow.com/questions/35618723/how-do-i-mock-the-documentclientexception-that-the-azure-documentdb-client-libra to Mock the DocumentClientException  class for unit testing since DocumentClientException does not have a public constructor. 

    private static DocumentClientException CreateDocumentClientExceptionForTesting(
                                           Error error, HttpStatusCode httpStatusCode)
    {
        var type = typeof (DocumentClientException);

        // we are using the overload with 3 parameters (error, responseheaders, statuscode)
       // use any one appropriate for you.

        var documentClientExceptionInstance = type.Assembly.CreateInstance(type.FullName,
            false, BindingFlags.Instance | BindingFlags.NonPublic, null,
            new object[] {error, (HttpResponseHeaders) null, httpStatusCode}, null, null);

        return (DocumentClientException)documentClientExceptionInstance;
    }

  Now, when we move to ASP .Net Core, this approach also does not work. The Assembly class has only two overloaded CreateInstance() methods.

        public object CreateInstance(string typeName);
        public object CreateInstance(string typeName, bool ignoreCase);

 The one is missing the CreateInstance() with the parameter of the arguments to the constructor and hence we cannot create the DocumentClientException instance for Unit Testing.

// The missing method in ASP .Net Core
        public virtual object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes);

  Why is this CreateInstance() method removed? How can I create Mock objects for such classes through reflection when there is no default constructor available?

Thanks,

Ranjith


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>