Giter Club home page Giter Club logo

Comments (3)

kzu avatar kzu commented on September 3, 2024

ยป dynamic o = obj; var output = o.DoStuff(input);

Why make obj dynamic? If you just invoke obj.DoStuff(input) it should just
work

/kzu from mobile

from moq4.

AmitGee avatar AmitGee commented on September 3, 2024

The code above is just to reproduce the issue. In the real application I'm using dynamic for implementing a multiple-dispatch pattern (visitor..), and stumbled on this error when unit testing it. I have multiple DoStuff() methods, each taking in a specialization of IInterface as its parameter. Using dynamic allows me to invoke the correct DoStuff method based on the runtime type of the IInterface object. I can post a longer sample that's closer to my application if that helps.
Thanks!!

from moq4.

stakx avatar stakx commented on September 3, 2024

@AmitGee: Moq actually handles this just fine. You might have noticed that the exception does not get thrown by the mock invocation, but by the return statement in your method:

private T GenericDoStuff<T>(IInterface<T> obj, T input)
{
    dynamic o = obj;
    var output = o.DoStuff(input);
    return output; // throws NullRef
}

That is, you have received a return value from the mock, and it is stored in output. From this point onward, Moq is out of the picture.

What happens next is that the Dynamic Language Runtime cannot convert the value in output directly to type T. See what happens when you replace return output; with return (T)(object)output;, i.e. when you first "cast" from dynamic to object, and then have a regular type cast from object to T. ;-)

from moq4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.