Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, you can avoid the conflict between <code>Moq</code> and <code>MSpec</code> by declaring</p> <pre><code>using Machine.Specifications; using Moq; using It = Machine.Specifications.It; </code></pre> <p>Then you'll only need to prefix with <code>Moq.</code> when you want to use Moq's <code>It</code>, for example <code>Moq.It.IsAny&lt;&gt;()</code>.</p> <hr> <p><strong>Onto your question.</strong></p> <p><em>Note: This is not the original answer but an edited one after the OP added some real example code to the question</em></p> <p>I've been trying out your sample code and I think it's got more to do with MSpec than Moq. Apparently (and I didn't know this either), when you modify the state of your SUT (System Under Test) inside an <code>It</code> delegate the changes gets remembered. What is happening now is:</p> <ol> <li><code>Because</code> delegate is run</li> <li><code>It</code> delegates are run, one after the other. If one changes the state, the following <code>It</code> <em>will never see the set up in the</em> <code>Because</code>. Hence your failed test.</li> </ol> <p>I've tried marking your spec with the <code>SetupForEachSpecificationAttribute</code>:</p> <pre><code>[Subject(typeof(object)), SetupForEachSpecification] public class When_Testing { // Something, Something, something... } </code></pre> <p>The attribute does as its name says: It will run your <code>Establish</code> and <code>Because</code> <em>before every</em> <code>It</code>. Adding the attribute made the spec behave as expected: 3 Successes, one fail (the verification that with Var = "two").</p> <p>Would the <code>SetupForEachSpecificationAttribute</code> solve your problem or is resetting after every <code>It</code> not acceptable for your tests?</p> <p>FYI: I'm using <code>Moq v4.0.10827.0</code> and <code>MSpec v0.4.9.0</code></p> <hr> <p>Free tip #2: If you're testing ASP.NET MVC apps with Mspec you might want to take a look at <a href="http://jamesbroo.me/introducing-machinespecificationsmvc/" rel="nofollow">James Broome's MSpec extensions for MVC</a></p>
    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. 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