IConfiguration and IConfigurationSection mocking (Microsoft.Extensions.Configuration)

3,997 views
Skip to first unread message

Rik Svendsen Rose

unread,
Sep 25, 2018, 3:39:26 AM9/25/18
to NSubstitute
Hi
The "new" Microsoft.Extensions.Configuration with IConfiguration and IConfigurationSection samed easy enough to mock. But something wierd happends:

var configuration = Substitute.For<IConfiguration>();
var section = Substitute.For<IConfigurationSection>();

 configuration.GetSection("Assets:Xzy").Returns(_sections);
_sections.Get<Asset[]>().Returns(assets);

But i get this, that i find very very strainght forward. Hav i gone blind, of have i made a weird mistake?:



Can not return value of type Asset[] for IDisposable.Dispose (expected type Void).

Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)),
and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).

If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member.
Return values cannot be configured for non-virtual/non-abstract members.

Correct use:
mySub.SomeMethod().Returns(returnValue);

Potentially problematic use:
mySub.SomeMethod().Returns(ConfigOtherSub());
Instead try:
var returnValue = ConfigOtherSub();
mySub.SomeMethod().Returns(returnValue);

David Tchepak

unread,
Sep 25, 2018, 4:03:39 AM9/25/18
to nsubs...@googlegroups.com
Hello,

Just to clarify, is `_sections` in your example code meant to be `section`? If `_sections` is not an `IConfigurationSection` that could be the problem.

Is this happening when a single test is run? Or is this only when a whole fixture/series of fixtures runs?
If it only happens with multiple tests then I think it is worth trying the beta Analyzers package: https://www.nuget.org/packages/NSubstitute.Analyzers.CSharp/
If you add that to your test project it can pick up some potential problems with non-virtual members in previous tests that could be breaking the test you have shown.

Please let me know how you go.

Regards,
David


--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at https://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/d/optout.

Rik Svendsen Rose

unread,
Sep 25, 2018, 4:38:12 AM9/25/18
to NSubstitute
 
Should have been: 

configuration.GetSection("Assets:Xzy").Returns(sections);
sections.Get<Asset[]>().Returns(assets); 

Typo in example here, not in code... Code uses _sections and _configuration private fields everywhere :-)

David Tchepak

unread,
Sep 25, 2018, 4:40:47 AM9/25/18
to nsubs...@googlegroups.com
I guessed so, just wanted to check. ;)

Let me know how you go with the Analysers and single vs multiple tests.


--

kévin

unread,
Mar 15, 2019, 12:21:44 PM3/15/19
to NSubstitute
Hello,

I'm facing the same issue, any update on this ?

Thx,

kévin

David Tchepak

unread,
Mar 15, 2019, 10:06:15 PM3/15/19
to nsubs...@googlegroups.com
Hi kévin,

Are you finding this happening when a single test is run? Or only when a whole fixture or series of fixtures runs?
Has the Analyzers package picked up any issues with the test suite?

Regards,
David

Message has been deleted

Max Onger

unread,
Feb 6, 2020, 4:37:59 PM2/6/20
to NSubstitute
For future people reading this, you should substitute the value of the section and not the section itself.

Bad:
conf.GetSection("bla").Returns("bla");

Good:
conf.GetSection("bla").Value.Returns("bla");

It should be enough to once call:
var conf = Substitute.For<IConfiguration>();
and then substitute every value you need, just like above.

Im pretty sure you can cascade it like this too:
conf.GetSection("bla").GetSection("bla").GetSection("bla").Value.Returns("bla");
Message has been deleted

Priyankar Dutta

unread,
Jul 8, 2020, 8:08:24 AM7/8/20
to NSubstitute
Did you ever manage to solve this problem?

Rik Svendsen Rose

unread,
Jul 8, 2020, 8:11:37 AM7/8/20
to nsubs...@googlegroups.com
I have not tested. Moved on to something else.
I Will try to find time

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages