Note that there are some explanatory texts on larger screens.

plurals
  1. POMoq Error: Cannot cast 'AnObject' to 'System.Delegate'
    primarykey
    data
    text
    <p>I have been trying to get past this problem for a couple of days now to no avail. I am trying to test the functionality of AnObject.AnAction (preemptive apologies, I have to obfuscate class/method names.) My goal is to keep MyTestObj.do_work(AnObject) from actually doing anything when it is called. It has code that I do want to be called in a unit test environment.</p> <p>Here is my Unit test code:</p> <pre><code>Mock&lt; MyTestObj &gt; myTestObj_mock = new Mock&lt; MyTestObj &gt; (); myTestObj_mock.Setup( e =&gt; e.do_work( It.IsAny&lt; AnObject &gt; () ) ); ... AnObject tester = new AnObject(); tester.anAction( myTestObj_mock.Object ); </code></pre> <p>within the method AnObject.AnAction( MyTestObj mto ):</p> <pre><code>... mto.do_work( this ); ... </code></pre> <p>It is at this point when ran that I get this error:</p> <pre><code>System.InvalidCastException : Unable to cast object of type 'myNamespace.AnObject' to type 'System.Delegate'. </code></pre> <p>MyTestObj.do_work is a virtual method.</p> <p>I have absolutely no idea why this error is coming up. I've stepped through the code as NUnit ran it and I am certain that is the point in which it crashes. It will not go any further into the code.</p> <p>I'm really stumped. Does anybody have any idea what is going on?</p> <p>Thanks in advance!</p> <hr> <p><em><strong></em>***</strong><em>Updates</em><strong><em>*</em>**</strong></p> <p>I tried to create an instance of AnObject within AnObject.AnAction and pass it instead of 'this':</p> <pre><code>... AnObject test = new AnObject(); mto.do_work( test ); ... </code></pre> <p>This did not have any effect.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload