NSubstitute: Trouble mocking a syntatic sugar getter method associated with a member variable with No corresponding setterHow to mock with NSubstitute a method with an array parameter?How to mock object's indexer with private setter in NSubstitute?NSubstitute mock extension methodNSubstitute mock a void method with out parametersHow to deal with Setter/Getter-Methods from Mocks?How to mock protected method with NSubstituteCan the MVC Controller method be mocked using the NSubstituteNSubstitute: Mock method is not returning expected resultNSubstitute returning NullReferenceException when Using ForPartsOf and mocking an Async Methodmock function with variable number of aguments using NSubstitute

Why is so much work done on numerical verification of the Riemann Hypothesis?

What (the heck) is a Super Worm Equinox Moon?

Why Shazam when there is already Superman?

Why is the "ls" command showing permissions of files in a FAT32 partition?

Mimic lecturing on blackboard, facing audience

Why is the Sun approximated as a black body at ~ 5800 K?

The Digit Triangles

Has the laser at Magurele, Romania reached a tenth of the Sun's power?

Is this toilet slogan correct usage of the English language?

Why should universal income be universal?

Why do ¬, ∀ and ∃ have the same precedence?

What fields between the rationals and the reals allow a good notion of 2D distance?

Is this part of the description of the Archfey warlock's Misty Escape feature redundant?

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

Microchip documentation does not label CAN buss pins on micro controller pinout diagram

How would you translate "more" for use as an interface button?

What is the highest possible scrabble score for placing a single tile

Does the Linux kernel need a file system to run?

Short story about a deaf man, who cuts people tongues

Creating two special characters

How many arrows is an archer expected to fire by the end of the Tyranny of Dragons pair of adventures?

How to draw a matrix with arrows in limited space

The IT department bottlenecks progress, how should I handle this?

awk assign to multiple variables at once



NSubstitute: Trouble mocking a syntatic sugar getter method associated with a member variable with No corresponding setter


How to mock with NSubstitute a method with an array parameter?How to mock object's indexer with private setter in NSubstitute?NSubstitute mock extension methodNSubstitute mock a void method with out parametersHow to deal with Setter/Getter-Methods from Mocks?How to mock protected method with NSubstituteCan the MVC Controller method be mocked using the NSubstituteNSubstitute: Mock method is not returning expected resultNSubstitute returning NullReferenceException when Using ForPartsOf and mocking an Async Methodmock function with variable number of aguments using NSubstitute













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?










share|improve this question

















  • 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 8 at 0:18












  • This looks more like an XY problem relating to the current design of your system.

    – Nkosi
    Mar 8 at 0:31















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?










share|improve this question

















  • 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 8 at 0:18












  • This looks more like an XY problem relating to the current design of your system.

    – Nkosi
    Mar 8 at 0:31













0












0








0


1






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?










share|improve this question














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?







unit-testing mocking nsubstitute stubbing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 0:01









crazyTechcrazyTech

170112




170112







  • 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 8 at 0:18












  • This looks more like an XY problem relating to the current design of your system.

    – Nkosi
    Mar 8 at 0:31












  • 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 8 at 0:18












  • This looks more like an XY problem relating to the current design of your system.

    – Nkosi
    Mar 8 at 0:31







1




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 8 at 0:18






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 8 at 0:18














This looks more like an XY problem relating to the current design of your system.

– Nkosi
Mar 8 at 0:31





This looks more like an XY problem relating to the current design of your system.

– Nkosi
Mar 8 at 0:31












1 Answer
1






active

oldest

votes


















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>






share|improve this answer

























  • 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 8 at 16:45






  • 1





    @crazyTech I definitely am not suggesting you wrap/adapt everything! A Facade 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 8 at 22:26











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054764%2fnsubstitute-trouble-mocking-a-syntatic-sugar-getter-method-associated-with-a-me%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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>






share|improve this answer

























  • 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 8 at 16:45






  • 1





    @crazyTech I definitely am not suggesting you wrap/adapt everything! A Facade 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 8 at 22:26
















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>






share|improve this answer

























  • 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 8 at 16:45






  • 1





    @crazyTech I definitely am not suggesting you wrap/adapt everything! A Facade 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 8 at 22:26














2












2








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>






share|improve this answer















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>







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 22:22

























answered Mar 8 at 2:02









David TchepakDavid Tchepak

6,97213250




6,97213250












  • 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 8 at 16:45






  • 1





    @crazyTech I definitely am not suggesting you wrap/adapt everything! A Facade 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 8 at 22:26


















  • 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 8 at 16:45






  • 1





    @crazyTech I definitely am not suggesting you wrap/adapt everything! A Facade 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 8 at 22:26

















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 8 at 16:45





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 8 at 16:45




1




1





@crazyTech I definitely am not suggesting you wrap/adapt everything! A Facade 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 8 at 22:26






@crazyTech I definitely am not suggesting you wrap/adapt everything! A Facade 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 8 at 22:26




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054764%2fnsubstitute-trouble-mocking-a-syntatic-sugar-getter-method-associated-with-a-me%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived