0

For my .NET C# application, I'm using a third-party e-faxing software named efaxdeveloper.com

I needed to mock efaxdeveloper.com's software OutboundResponse object.

Please keep in mind that since it's 3rd party, I obviously can Not modify the 3rd-party dlls.

In the eFaxDeveloper.dll, the following is the code for the OutboundResponse class:

using System.Runtime.InteropServices;

namespace J2.eFaxDeveloper.Outbound
{
    //
    // Summary:
    //     oubound response
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [System.Runtime.Serialization.DataContractAttribute(Namespace = "")]
    public class OutboundResponse
    {
        public OutboundResponse();

        //
        // Summary:
        //     Unique client specified transmission identifier
        public string TransmissionID { get; }
        //
        // Summary:
        //     eFax Developer™ transmission identifier
        public string DOCID { get; }
        //
        // Summary:
        //     J2.eFaxDeveloper.Outbound.StatusCode
        public StatusCode StatusCode { get; }
        //
        // Summary:
        //     Status description
        public string StatusDescription { get; }
        //
        // Summary:
        //     J2.eFaxDeveloper.Outbound.ErrorLevel
        public ErrorLevel ErrorLevel { get; }
        //
        // Summary:
        //     Error message
        public string ErrorMessage { get; }
    }
}

Since it only has getters, I tried the following snippet of code:

    OutboundResponse outboundResponseInQuestion = Substitute.For<OutboundResponse>();

    outboundResponseInQuestion.TransmissionID.Returns("someTransmissionID");

Unfortunately, outboundResponseInQuestion.TransmissionID throws

'outboundResponseInQuestion.TransmissionID' threw an exception of type 'System.NullReferenceException'

I can Not create an Interface for the OutboundResponse class so could someone please tell me how I can mock said object using NSubstitute and make it return the proper values?

crazyTech
  • 1,379
  • 3
  • 32
  • 67
  • 1
    You are trying to mock 3rd party dependencies that you have no control over and still expect miracles. Why are you unable to abstract that dependency out? This is one of the risks with tightly coupling to code you can't control. – Nkosi Mar 08 '19 at 00:18
  • This looks more like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) relating to the current design of your system. – Nkosi Mar 08 '19 at 00:31

1 Answers1

2

NSubstitute can not mock this type because it does not have virtual members. (We also can't manually create a sub-type of OutboundResponse that overrides the getters and exposes setters and use that for testing, for the same reason.)

You may have an easier time by creating an interface that encapsulates the entirety of the required behaviour from the 3rd party library (facade pattern) and testing your code's interaction with that interface. You can then separately test your implementation of that interface produces correct results when calling the 3rd party library. These may be integration or manual tests.

<shamelessplug>I have previously written a bit about the downsides of mocking types we don't own that you may find useful.</shamelessplug>

David Tchepak
  • 9,826
  • 2
  • 56
  • 68
  • Thx for answer. Fortunately, the public API for the 3rd-party E-Fax Developer software is relatively small. However, R u saying it would be good for Unit testing if I create a Wrapper(Or Adapter) corresponding to every public Class, Enum, Struct etc., belonging to the 3rd-party technology module in question? Is that correct? – crazyTech Mar 08 '19 at 16:45
  • 1
    @crazyTech I definitely am not suggesting you wrap/adapt everything! A [Facade](https://en.wikipedia.org/wiki/Facade_pattern) provides a simple interface to a more complex underlying system. For example, an `IFaxService` interface with a single `SendFax` method. You could then mock this interface to test your app code. The production implementation of the interface would call the 3rd party fax library, dealing with details like `OutboundResponse`. This code will still need to be tested in some other way, but it enables you to test you app code much more easily. – David Tchepak Mar 08 '19 at 22:26