Home Products Download Learn Buy Now Why Typemock? Support & Community About
Community > Forum
 FAQFAQ    SearchSearch   RegisterRegister   RssRss   ProfileProfile   Log inLog in 

Can you fake out System.Reflection.Assembly ?

 
Post new topic   Reply to topic    Typemock Forum Index -> Unit Testing with Isolator (Previously called TypeMock)
View previous topic :: View next topic  
Author Message
rfennell
Advanced
Advanced


Joined: 13 Nov 2008
Posts: 19

PostPosted: Wed Feb 11, 2009 6:04 pm    Post subject: Can you fake out System.Reflection.Assembly ? Reply with quote

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
View user's profile Send private message
ohad
Site Admin
Site Admin


Joined: 18 May 2006
Posts: 683

PostPosted: Wed Feb 11, 2009 8:28 pm    Post subject: Reply with quote

Hi

Actually the exception message is true Sad
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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Typemock Forum Index -> Unit Testing with Isolator (Previously called TypeMock) All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group