Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was finally able to get to a compiler and play with the code and here is what I found. It is basically what Giles said above for his first part of the question. </p> <p>Here is the code hint for Setup:</p> <blockquote> <p>Specifies a setup on the mocked type for a call to a value returning method.</p> </blockquote> <p>And, here is the actual code for Setup</p> <pre><code>public ISetup&lt;T&gt; Setup(Expression&lt;Action&lt;T&gt;&gt; expression) </code></pre> <p>So, Setup is actually setting up the code as an Action (a delegate, basically). When the test runner is run, it hits this point and usually expects to feed the delegate to the Returns method</p> <pre><code>public IReturnsResult&lt;TMock&gt; Returns(TResult value) </code></pre> <p>So, this is never done, and instead of Mock running through the whole expected path and negotiating all of the code out, then it returns mistyped values. </p> <p>Sooo, this is basically where Giles explanation is correct. If you are testing that the do_work method is being called, then you want to use <code>Verify</code> (which truly uses your fake object as a mock). However, if not, then you have no need to even set this up as it does nothing. If that is the case, then maybe that piece of code should not be in that specific code block, but you would be best to evaluate that. </p> <p>And, finally, the Setup method should only be used if you are truly using your object as a stub, simply using it to inject logic into your code.</p> <p>Hopefully, that makes sense and helps you with your problem.</p> <p>Also, a <code>Callback</code> might be of use here depending on what you are doing. But, I am not sure. They have many examples in the <a href="http://code.google.com/p/moq/wiki/QuickStart" rel="nofollow">Moq quickstart</a></p> <p><strong>UPDATE FOR MY ATTEMPT AT RECREATING using Version 4.0.10827...and had no issues</strong></p> <pre><code> [Test] public void teststuff() { 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>...</p> <pre><code>public class MyTestObj { public virtual void do_work(AnObject o) { } } public class AnObject { public void anAction(MyTestObj obj) { obj.do_work(new AnObject()); } } </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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