Comments (3)
ยป 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.
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.
@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)
- Stop using Moq as a guinea pig to get feedback on and develop SponsorLink HOT 105
- Support Static Abstract property HOT 1
- Community Involvement HOT 5
- Change log is not updated since version 4.18.4 HOT 3
- Mocking Blobclient.DownloadContentAsync() HOT 3
- Poll on SponsorLink HOT 1
- Should allow null references as constructor parameters. HOT 1
- Replacement for Moq.MethodCallReturns'2 HOT 3
- The public API is still missing nullable reference type annotations HOT 1
- a minor changes to improve the readability HOT 2
- Mock verification slow with It.Is(Expression) HOT 5
- [Feature Request] Make it possible to moq concrete classes HOT 1
- Moq dependency uprev is not working as expected HOT 1
- SponsorLink Integration Makes Moq Unuasable due to PII and GDPR Requirements HOT 9
- Add the ability to compare ref argument by value HOT 1
- Cannot cast inside the Action<T> parameter of Verify method
- Wrong Setup by using String interpolation within It.Is<string> HOT 4
- Question: would it make sense to call verify all on a strict mock when the mock gets disposed?
- `DefaultValueProvider` extensibility
- Some source code files is filled with comments like: "Unmerged change from project "
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moq4.