Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think this is what you want, I already tested this code and works</p> <p>The tools used are: (all these tools can be downloaded as Nuget packages)</p> <p><a href="http://fluentassertions.codeplex.com/" rel="nofollow noreferrer">http://fluentassertions.codeplex.com/</a></p> <p><a href="http://autofixture.codeplex.com/" rel="nofollow noreferrer">http://autofixture.codeplex.com/</a></p> <p><a href="http://code.google.com/p/moq/" rel="nofollow noreferrer">http://code.google.com/p/moq/</a></p> <p><a href="https://nuget.org/packages/AutoFixture.AutoMoq" rel="nofollow noreferrer">https://nuget.org/packages/AutoFixture.AutoMoq</a></p> <pre><code>var fixture = new Fixture().Customize(new AutoMoqCustomization()); var myInterface = fixture.Freeze&lt;Mock&lt;IFileConnection&gt;&gt;(); var sut = fixture.CreateAnonymous&lt;Transfer&gt;(); myInterface.Setup(x =&gt; x.Get(It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;())) .Throws&lt;System.IO.IOException&gt;(); sut.Invoking(x =&gt; x.TransferFiles( myInterface.Object, It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;() )) .ShouldThrow&lt;System.IO.IOException&gt;(); </code></pre> <p><strong>Edited:</strong></p> <p>Let me explain:</p> <p>When you write a test, you must know exactly what you want to test, this is called: "subject under test (SUT)", if my understanding is correctly, in this case your SUT is: <code>Transfer</code></p> <p>So with this in mind, you should not mock your SUT, if you substitute your SUT, then you wouldn't be actually testing the real code</p> <p>When your SUT has external dependencies (very common) then you need to substitute them in order to test in <strong>isolation</strong> your SUT. When I say substitute I'm referring to use a mock, dummy, mock, etc depending on your needs</p> <p>In this case your external dependency is <code>IFileConnection</code> so you need to create mock for this dependency and configure it to throw the exception, then just call your SUT real method and assert your method handles the exception as expected</p> <ul> <li><p><code>var fixture = new Fixture().Customize(new AutoMoqCustomization());</code>: This linie initializes a new Fixture object (Autofixture library), this object is used to create SUT's without having to explicitly have to worry about the constructor parameters, since they are created automatically or mocked, in this case using Moq</p></li> <li><p><code>var myInterface = fixture.Freeze&lt;Mock&lt;IFileConnection&gt;&gt;();</code>: This freezes the <code>IFileConnection</code> dependency. Freeze means that Autofixture will use always this dependency when asked, like a singleton for simplicity. But the interesting part is that we are creating a Mock of this dependency, you can use all the Moq methods, since this is a simple Moq object</p></li> <li><p><code>var sut = fixture.CreateAnonymous&lt;Transfer&gt;();</code>: Here AutoFixture is creating the SUT for us</p></li> <li><p><code>myInterface.Setup(x =&gt; x.Get(It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;())).Throws&lt;System.IO.IOException&gt;();</code> Here you are configuring the dependency to throw an exception whenever the <code>Get</code> method is called, the rest of the methods from this interface are not being configured, therefore if you try to access them you will get an unexpected exception</p></li> <li><p><code>sut.Invoking(x =&gt; x.TransferFiles(myInterface.Object, It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;())).ShouldThrow&lt;System.IO.IOException&gt;();</code>: And finally, the time to test your SUT, this line uses the FluenAssertions library, and it just calls the <code>TransferFiles</code> <strong>real method from the SUT</strong> and as parameters it receives the mocked <code>IFileConnection</code> so whenever you call the <code>IFileConnection.Get</code> in the normal flow of your SUT <code>TransferFiles</code> method, the mocked object will be invoking throwing the configured exception and this is the time to assert that your SUT is handling correctly the exception, in this case, I am just assuring that the exception was thrown by using the <code>ShouldThrow&lt;System.IO.IOException&gt;()</code> (from the FluentAssertions library)</p></li> </ul> <p>References recommended:</p> <p><a href="http://martinfowler.com/articles/mocksArentStubs.html" rel="nofollow noreferrer">http://martinfowler.com/articles/mocksArentStubs.html</a></p> <p><a href="http://misko.hevery.com/code-reviewers-guide/" rel="nofollow noreferrer">http://misko.hevery.com/code-reviewers-guide/</a></p> <p><a href="http://misko.hevery.com/presentations/" rel="nofollow noreferrer">http://misko.hevery.com/presentations/</a></p> <p><a href="http://www.youtube.com/watch?v=wEhu57pih5w&amp;feature=player_embedded" rel="nofollow noreferrer">http://www.youtube.com/watch?v=wEhu57pih5w&amp;feature=player_embedded</a></p> <p><a href="http://www.youtube.com/watch?v=RlfLCWKxHJ0&amp;feature=player_embedded" rel="nofollow noreferrer">http://www.youtube.com/watch?v=RlfLCWKxHJ0&amp;feature=player_embedded</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