Asserting an exception thrown from a mock object constructor
The constructor will not be called until you reference mock.Object. That should trigger the exception you're expecting.
On a side note, it's generally bad practice to have a constructor throw exceptions other than usage exceptions (such as the various ArgumentException derivatives.) Most developers don't expect 'new' to throw an exception unless they've done something very wrong; a file not existing is the kind of exception that can legitimately happen beyond the control of the program, so you might want to make this a static factory method instead like "FromFileName". EDIT: Given that this is a base class constructor, that's not really applicable either, so you may want to consider where is the best place to institute this check. After all, the file may cease existing at any point, so it might not even make sense to check in the constructor (you'll need to check in all relevant methods anyway.)
I faced similar problem today. I worked it out using the following solution:
[Test]
[ExpectedException(typeof(System.IO.FileNotFoundException))]
public void MyFileType_CreationWithNonexistingPath_ExceptionThrown()
{
String nonexistingPath = "C:\\does\\not\\exist\\file.ext";
var mock = new Mock<MyFileType>(nonexistingPath);
try
{
var target = mock.Object;
}
catch(TargetInvocationException e)
{
if (e.InnerException != null)
{
throw e.InnerException;
}
throw;
}
}