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
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
add a comment |
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
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
add a comment |
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
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
unit-testing mocking nsubstitute stubbing
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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>
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, anIFaxService
interface with a singleSendFax
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 likeOutboundResponse
. 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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, anIFaxService
interface with a singleSendFax
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 likeOutboundResponse
. 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
add a comment |
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>
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, anIFaxService
interface with a singleSendFax
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 likeOutboundResponse
. 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
add a comment |
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>
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>
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, anIFaxService
interface with a singleSendFax
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 likeOutboundResponse
. 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
add a comment |
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, anIFaxService
interface with a singleSendFax
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 likeOutboundResponse
. 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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