It is currently Sat May 25, 2013 1:24 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Feb 11, 2009 6:04 pm 
Offline
Advanced
Advanced

Joined: Thu Nov 13, 2008 4:33 pm
Posts: 23
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());
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 8:28 pm 
Offline
Site Admin
Site Admin

Joined: Thu May 18, 2006 12:03 pm
Posts: 904
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
[ Time : 0.058s | 13 Queries | GZIP : Off ]