Note that there are some explanatory texts on larger screens.

plurals
  1. POMocking a method to throw an exception (moq), but otherwise act like the mocked object?
    primarykey
    data
    text
    <p>I have a <code>Transfer</code> class, simplified it looks like this:</p> <pre><code>public class Transfer { public virtual IFileConnection source { get; set; } public virtual IFileConnection destination { get; set; } public virtual void GetFile(IFileConnection connection, string remoteFilename, string localFilename) { connection.Get(remoteFilename, localFilename); } public virtual void PutFile(IFileConnection connection, string localFilename, string remoteFilename) { connection.Get(remoteFilename, localFilename); } public virtual void TransferFiles(string sourceName, string destName) { source = internalConfig.GetFileConnection("source"); destination = internalConfig.GetFileConnection("destination"); var tempName = Path.GetTempFileName(); GetFile(source, sourceName, tempName); PutFile(destination, tempName, destName); } } </code></pre> <p>The simplified version of the <code>IFileConnection</code> interface looks like this:</p> <pre><code>public interface IFileConnection { void Get(string remoteFileName, string localFileName); void Put(string localFileName, string remoteFileName); } </code></pre> <p>The real class is supposed to handle a <code>System.IO.IOException</code> that is thrown when the <code>IFileConnection</code> concrete classes loses connectivity with the remote, sending out emails and what not.</p> <p>I would like to use Moq to create a <code>Transfer</code> class, and use it as my concrete <code>Transfer</code> class in all properties and methods, except when the <code>GetFile</code> method is invoked - then I want it to throw a <code>System.IO.IOException</code> and make sure the <code>Transfer</code> class handles it properly.</p> <p>Am I using the right tool for the job? Am I going about this the right way? And how would I write the setup for that unit test for <code>NUnit</code>?</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.
 

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