| View previous topic :: View next topic |
| Author |
Message |
rfennell Advanced

Joined: 13 Nov 2008 Posts: 16
|
Posted: Wed Feb 11, 2009 6:04 pm Post subject: Can you fake out System.Reflection.Assembly ? |
|
|
I have been trying to test some code that uses reflection to loads assemblies. The problem I have is I want to inject some exceptions to test when I do not have the rights to load the assembly.
I have not been able to get it to work, is the following syntax possible or will I keep hitting the problem that the Assembly class is part of mscorelib and so not mockable?
| Code: |
[TestMethod, Isolated]
[ExpectedException(typeof(System.Security.SecurityException))]
public void GetExecutingAssemblyMockTest()
{
Isolate.WhenCalled(() => System.Reflection.Assembly.GetExecutingAssembly()).WillThrow(new System.Security.SecurityException());
var * = System.Reflection.Assembly.GetExecutingAssembly();
Assert.Fail(*.GetName().ToString());
}
|
|
|
| Back to top |
|
 |
ohad Site Admin

Joined: 18 May 2006 Posts: 617
|
Posted: Wed Feb 11, 2009 8:28 pm Post subject: |
|
|
Hi
Actually the exception message is true
The Isolator cannot fake types that are defined in mscorlib.dll.
This is the only case where you have to change the code in order to fake it.
e.g:
| Code: |
public class AssemblyWrapper
{
public static Assembly GetExecutingAssembly()
{
return Assembly.GetExecutingAssembly();
}
}
[TestClass]
public class TestClass
{
[TestMethod, Isolated]
[ExpectedException(typeof(System.Security.SecurityException))]
public void GetExecutingAssemblyMockTest()
{
Isolate.WhenCalled(() => AssemblyWrapper.GetExecutingAssembly()).WillThrow(new System.Security.SecurityException());
var assembly = AssemblyWrapper.GetExecutingAssembly();
Assert.Fail(assembly.GetName().ToString());
}
}
|
_________________ Regards
Ohad,
TypeMock Support Group |
|
| Back to top |
|
 |
|