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

Forgetting the musical notes while performing in concert

Why does Arabsat 6A need a Falcon Heavy to launch

How do I write bicross product symbols in latex?

What is the most common color to indicate the input-field is disabled?

What's the difference between 'rename' and 'mv'?

Facing a paradox: Earnshaw's theorem in one dimension

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

Could gravitational lensing be used to protect a spaceship from a laser?

What do you call someone who asks many questions?

Will google still index a page if I use a $_SESSION variable?

How can saying a song's name be a copyright violation?

How to take photos in burst mode, without vibration?

Modeling an IP Address

Combinations of multiple lists

Why does Kotter return in Welcome Back Kotter

Watching something be written to a file live with tail

Is the Joker left-handed?

What is the word for reserving something for yourself before others do?

How can I make my BBEG immortal short of making them a Lich or Vampire?

Did Shadowfax go to Valinor?

Does a druid starting with a bow start with no arrows?

Is it legal for company to use my work email to pretend I still work there?

Infinite Abelian subgroup of infinite non Abelian group example

Arrow those variables!



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






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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


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














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

7,01713250




7,01713250












  • 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

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved