Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At first blush it looks like it should work but as it is not you might try :</p> <p>Change the matching conditions on the verify to <em>It.IsAny(), It.IsAny()</em> to see if it is called at all. - If not, debug through the code and see what is happening - If it does match, then it is a matter of the matching</p> <p>Try <em>entity.Object, It.IsAny()</em> to see if the UserInputEntity is wrong.</p> <p>If the UserInputEntity is fine then put a callback on the source Setup. It doesn't need to do anything but will allow you to check which values are being used when SetupCsvFileVerification is called.</p> <p><strong>UPDATE</strong> <br/> I think I found it, and it relates to one of my points below. You initialize <em>cts</em> to a value. It needs to have an initial value because the setups </p> <pre><code>source.SetupGet(x =&gt; x.Source).Returns(cts); source.SetupGet(x =&gt; x.Token).Returns(cts.Token); </code></pre> <p>fail with out it because it evaluates cts.Token immediately. It means that this will return the token from the CancellationTokenSource defined in the test <em>not</em> the one defined in the production code (and stored using the callback).</p> <p>To ensure that you use the new cts value you should change the setups to </p> <pre><code>source.SetupGet(x =&gt; x.Source).Returns(() =&gt; cts); source.SetupGet(x =&gt; x.Token).Returns(() =&gt; cts.Token); </code></pre> <p>which will defer evaluation until used i.e. after the callback from the set has executed.</p> <p><strong>Added Details</strong> <br /> The problem is timing of evaluation</p> <p>Say in the test setup you create a cts X which has a token A. <br /> You then setup the Source to return the cts and the token to return cts.Token <br /> These are evaluted <strong>now</strong>, the gets are told to return X and A. <br /></p> <p>When running, the cts is overwritten by the one set using the callback (call it Y with a Token of B) and it is the B value that the Verify uses and thus fails.</p> <p><br /> By changing the setups to use lambdas we are telling them to use <em>'whatever value cts is pointing at when you get called'</em> so the sequence is now</p> <p>Setup up get - do not evaluate CTS or token values Call validation <br /> - set cts using callback <br /> - get source => evaluate, use newly set value (Y) <br /> - get token => evaluate, use newly set value (B) <br /> Verify => compare against B, passes <br /></p> <p><strong>Other ideas</strong></p> <p>Does the CancellationTokenSource exist only to support testing?</p> <p>If so, another approach is to try replacing this with an <strong>ICancellationTokenProvider</strong> which would replace the <em>new CancellationTokenSource();</em> call in your production code. This would allow you to more easily inject a specific CancellationToken into the code and thus verify SetupCsvFileVerification().</p> <p><strong>Minor quibbles - ignore at will</strong></p> <p>Also, unless you are using a strict behaviour the setup</p> <pre><code>verify.Setup(x =&gt; x.SetupCsvFileVerification(It.IsAny&lt;UserInputEntity&gt;(), It.IsAny&lt;CancellationToken&gt;())); </code></pre> <p>is superfluous. It doesn't return anything, so it is not useful as a stub, and you are explicitly verifying the call later on.</p> <p>Likewise the initialization of 'cts' to a value is not needed, setting it to null should suffice as the callback on setting the source should populate it.</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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