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;
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
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, 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