Search code examples
unit-testingfaultexceptionexpected-exception

Unit Test expects System.ServiceModel.FaultException


This is my Unit Test:

    [TestCategory("Repo")]
    [TestMethod]
    [ExpectedException(typeof(System.ServiceModel.FaultException))]
    public void RepoUnitTest_ExpectError()
    {
        var repo = new CreateClientRepository(ClientServiceWrapper);

        IClientObject input = new ClientObject
        {
            ClientId = 0,
            ClientName = null
        };

        repo.CreateClient(input);
    }

I am providing invalid input to my client repository which in turn calls a third party client service, and I expect the client service to throw an error. And, client service throws an exception too, but not the way I expect it. I expect it "System.ServiceModel.FaultException", but it give me this:

Test method RepoUnitTest_ExpectError threw exception 
System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],
but exception System.ServiceModel.FaultException was expected.

Not sure what do I put in "ExpectedException" so this Unit Test passes with correctly expected exception.


Solution

  • Guess I should have waited few more minutes before posting this question, but I am glad did, now I can share the knowledge with whoever stumbles upon similar issue.

    The expected exception should be written this way (for this particular case):

    [ExpectedException(typeof(System.ServiceModel.FaultException<
    System.ServiceModel.ExceptionDetail>))]