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
I have a solution where every single project is strongly signed. I have been using TypeMock 5.3.4.0, and everything works fine until I refactor my test code and move the live code methods into another assembly. They are still under test being handed Mock objects grant you but I get a TypeLoadException could not load type.... when I do this, if I move the code back into the testing assembly it works fine. Whats the deal? This is a huge problem since I can only test code in my test assemblies at the moment!!

Here is the code broken down by assembly:

MyAssembly.Something.Tests:

        SPWeb fakeWeb;
        SPFeatureReceiverProperties fakeFeatureReceiverProperties;
        SPList fakeList;

        [TestInitialize]
        public void Initialize()
        {
            fakeWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
            fakeFeatureReceiverProperties = Isolate.Fake.Instance<SPFeatureReceiverProperties>(Members.ReturnRecursiveFakes);
            fakeList = Isolate.Fake.Instance<SPList>(Members.ReturnRecursiveFakes);

            Isolate.WhenCalled(() => fakeFeatureReceiverProperties.Feature.Parent).WillReturn(fakeWeb);
        }

[TestMethod]
        [Isolated]
        public void ShouldCheckIfMyProductLibraryListAlreadyExistsBeforeTryingToAdd()
        {
            const string ListItemTitle = "My Product Library";
            var fakeListItem = fakeWeb.Lists[""].Items.Add();

            Isolate.WhenCalled(() => fakeListItem.Title).WillReturn(ListItemTitle);
            Isolate.WhenCalled(() => fakeWeb.Lists).WillReturnCollectionValuesOf(new[] { fakeListItem });

            CreateMyProductLibrary(fakeFeatureReceiverProperties, ListItemTitle);
            Isolate.Verify.WasNotCalled(() => fakeWeb.Lists.Add("", "", SPListTemplateType.DocumentLibrary));
        }

 private void CreateMyProductLibrary(SPFeatureReceiverProperties properties, string listName)
        {
            using (SPWeb web = properties.Feature.Parent as SPWeb)
            {
                if (!SPHelpers.ListExists(listName, web.Lists))
                {
                    web.Lists.Add(listName, "test", SPListTemplateType.DocumentLibrary);
                }
            }
        }




MyAssembly.Something:

public static class SPHelpers
    {
        
        public static bool ListExists(string name, SPListCollection listCollection)
        {
            foreach (SPListItem list in listCollection)
            {
                if (list.Title == name)
                {
                    return true;
                }
            }
            return false;
        }
    }



It errors out on the SPHelpers, saying it cant load that type...Which is just bizarre!

TestCase 'MyAssembly.Something.Tests.ShouldCreateMyProductLibraryList'
failed: System.TypeLoadException: Could not load type 'MyAssembly.Something.SPHelpers' from assembly 'MyAssembly.Something, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3ab129e6374ce594'.
at MyAssembly.Something.Tests.FeatureReceiverTests.CreateMyProductLibrary(SPFeatureReceiverProperties properties, String listName)
C:WorkMyJMyAssembly.Something.TestsFeatureActivationTests.cs(43,0): at MyAssembly.Something.Tests.FeatureReceiverTests.ShouldCreateMyProductLibraryList()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
C:WorkMyJMyAssembly.Something.TestsFeatureActivationTests.cs(40,0): at MyAssembly.Something.Tests.FeatureReceiverTests.ShouldCreateMyProductLibraryList()
asked by emalamisura (1.2k points)

3 Answers

0 votes
Hi,
Indeed this is strange!

I couldn't reproduce the error you reported.
Lets try to isolate the problem:
Try to call SPHelpers.ListExists from the test project without using the Isolator and see if you get the same results.

[TestMethod]
public void Test()
{
    SPHelpers.ListExists("", null);
}


When you run this test please disable the Isolator from the Tools menu to make sure that the Isolator is not involved.
Also please tell me, is the test project assembly is signed?
answered by ohad (35.4k points)
0 votes
Hi I will try that, but it succeeds usually if I move that code into the test assembly in any case. But yes the test project is signed, it wasn't at first and then I was oh well that must be the issue so I signed it and still same. I also tried Enabling/Disabling TypeMock, restarting in case something was holding onto a file handle. I tried Adding the TypeMock assemblies to the other project just to see, also I tried adding InternalsVisibleTo which should not matter and it obviously didn't help....

I can try to reproduce in a test solution...
answered by emalamisura (1.2k points)
0 votes
Ok so apparently this is not TypeMock related, and somehow my solution has gotten corrupt? On StackOverflow the only way to solve this for someone was to recreate the entire solution....hmmmm
answered by emalamisura (1.2k points)
...