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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page