chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
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?

[TestMethod, Isolated]
[ExpectedException(typeof(System.Security.SecurityException))]
public void GetExecutingAssemblyMockTest()
{
            Isolate.WhenCalled(() => System.Reflection.Assembly.GetExecutingAssembly()).WillThrow(new System.Security.SecurityException());

            var ass = System.Reflection.Assembly.GetExecutingAssembly();
            Assert.Fail(ass.GetName().ToString());
}
asked by rfennell (6.8k points)

1 Answer

0 votes
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:
    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());
        }
    }
answered by ohad (35.5k points)
...