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
0 votes
Hi,

I have an static class that is calling an service.
And I want to mock the GetNextKey Method call of that class.
public static class KeyServerWorkaround
    {
        private static readonly WebKeyServiceSoapClient KeyServiceClient = new WebKeyServiceSoapClient();
        private static readonly string DbSuffix = ConfigurationManager.AppSettings["keyServerGuDbAlias"];

        public static long GetNextKey(object entity)
        {
            object[] erg = entity.GetType().GetCustomAttributes(typeof(DatabaseObjectTableAttribute), false);
            if(erg.Length != 1)
            {
                throw new ArgumentException("DatabaseObjectTableAttribute für Objekt " + entity.GetType().FullName + " fehlt.", "entity");
            }
            DatabaseObjectTableAttribute attr = (DatabaseObjectTableAttribute)erg[0];
            return KeyServiceClient.getNextKey(DbSuffix, attr.Table, attr.IdColumn);
        }
    }


My (simplified) UnitTestMethos looks like
[TestMethod]
        [Isolated]
        public void InsertDatabaseObjectTest()
        {
            const long nextKey = 1234567L;
           
            Isolate.Fake.StaticMethods(typeof(KeyServerWorkaround), Members.MustSpecifyReturnValues);
            Isolate.WhenCalled(() => KeyServerWorkaround.GetNextKey(null)).WillReturn(nextKey); //the Exception is raised here.


//Do something
        }


Surprisingly I get that Exception
Test method WKO.Inhouse.DatabaseIO.Tests.DatabaseAccessTest.InsertDatabaseObjectTest threw exception: TypeMock.TypeMockException:
*** Cannot mock types from mscorlib assembly.

Well but I'm pretty sure the class is not in the mscorlib
asked by swtrse (4k points)

1 Answer

0 votes
My fault.
I forgot to mock the static Constructor so that the initialisation will not happen, with adding
Isolate.Fake.StaticConstructor(typeof(KeyServerWorkaround));

to the testcode all works fine
answered by swtrse (4k points)
...