Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot mock an interface method that returns a generic collection with dynamic type #747

Closed
jeohearn opened this issue Oct 25, 2023 · 5 comments
Labels
feature-request Request for a new NSubstitute feature

Comments

@jeohearn
Copy link

Describe the bug

Get an exception when mocking an interface method that returns a generic collection with a dynamic type.

To Reproduce

The code below cannot execute without throwing an exception, i.e.

Exception thrown: 'NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException' in NSubstitute.dll
`
public class Demo
{
public interface IAccess
{
Dictionary<string, dynamic>? Execute(string name, Dictionary<string, dynamic>? parameters);
}

[Test]
public void MethodReturnsGenericCollectionHavingDynamicType()
{
    var access = Substitute.For<IAccess>();
    dynamic dyn = new ExpandoObject();
    dyn.Text = "value";
    Dictionary<string, dynamic> ret = new Dictionary<string, dynamic> {{"sp1", dyn}};
    access.Execute(Arg.Any<string>(), null).Returns(ret);  // Get exception documented earlier
    // Or
    SubstituteExtensions.Returns(access.Execute(Arg.Any<string>(), null), ret); // Get exception documented earlier

    // remaining code omitted

}

}
`
Environment:

  • NSubstitute version: [e.g. 5.0..0]
  • NSubstitute.Analyzers version: [e.g. CSharp 1.0.16]
  • Platform: [e.g. .NET 7 project on Windows]

Additional context
I have tried casting Dictionary<string, dynamic> to Dictionary<string, object> without success, i.e.

access.Execute(Arg.Any(), null).Returns((Dictionary<string, object>)ret);

as well as,

SubstituteExtensions.Returns(access.Execute(Arg.Any(), null), (Dictionary<string, object>)ret);

@dtchepak
Copy link
Member

Hi @jeohearn ,

Does the explanation in this thread cover the case you have found?

#144 (comment)

@jeohearn
Copy link
Author

I used all the work around techniques from the thread mentioned but they did not resolve my issue. I had to abandon using NSubstitute in favor of stubs which I really wanted to avoid.

@304NotModified
Copy link
Contributor

Doubting if this a bug or if we need a support for dynamics. As I don't see any docs about dynamics, I'm tagging this a feature request. But please correct me if wrong.

@304NotModified 304NotModified added the feature-request Request for a new NSubstitute feature label Apr 29, 2024
@dtchepak
Copy link
Member

@304NotModified from #144 (comment) I think we don't have a way to address this. Will close for now; please re-open if anyone knows an implementation that will work for this.

@304NotModified
Copy link
Contributor

To avoid confusing, I'm marking this as not plannend instead of completed :)

@304NotModified 304NotModified closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for a new NSubstitute feature
Projects
None yet
Development

No branches or pull requests

3 participants