Creating an instance of the accessor class

I am currently writing a unit test framework, which should run standard unit tests written in Visual Studio at the end. Framework currently does not work with accessories. Consider the following testing method:

[TestMethod()]
public void TestMethod()
{
      ExampleMethods_Accessor target = null;
      target = new ExampleMethods_Accessor();
      target.SomeMethod();
}

In this example, an accessor was generated by Visual Studio. unit test works great on startup using the Visual Studio Unit Testing Environment. However, I would like to call TestMethod () from my Framework. The following exception was thrown in the line "target = new ExampleMethods_Accessor ()":

The type initializer for "Proband.ExampleMethods_Accessor" threw an exception.

Internal exception:

Could not load file or assembly: Proband, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null ...

- , Microsoft Unit Testing Framework ? , - TestContext. "null" . unit test Visual Studio TestContext . , ? ?

, Christian

EDIT:

, . ILSpy, , Proband_Accessor.dll. , , :

SomeClass_Accessor.m_privateType = new PrivateType("Probant, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Probant.SomeClass");

unit test ( ):

    [TestMethod()]
    [DeploymentItem("Proband.dll")]
    public void SomeMethodTest()
    {
        ExampleMethods_Accessor target = null;
        ExampleMethods c = null;

        try
        {
            Assembly.Load("Proband, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); // this works fine
            PrivateType tx = new PrivateType(typeof(ExampleMethods)); // this works fine as well (also without loading the assembly)

            PrivateType t = new PrivateType("Proband, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Proband.ExampleMethods"); // this causes the exception

            c = new ExampleMethods(); // this works fine
            target = new ExampleMethods_Accessor(); // this causes the exception as well
        }
        catch (Exception ex)
        {
            Console.WriteLine();
        }
        int actual;
        actual = target.SomeMethod();
    }

, " PrivateType (" Proband, Version.... " . - ?

+5
3

.

AppDomain AssemblyResolveEventHandler:

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

:

    private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
    {
        if(args.Name == "Proband, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
        {
            // resolving correct assembly of type under test
            return typeof(ExampleMethods).Assembly;
        }
        else
        {
            return null;
        }
    }

"target = new ExampleMethods_Accessor();" .

, .

, - : , -:)

+1

, , DeploymentItem . , .

[TestMethod]
[DeploymentItem("FedImportServer.dll")]  // ** This is necessary for the build machine. **
public void SourceFileStillExistsAfterProcessingFails()

. .

:

Test method FedImportTests.FedImportServiceHostTest.FileNoLongerExistsAfterSucessfulProcessing threw exception: System.TypeInitializationException: The type initializer for 'FedImportServer.Processing.FileProcessor_Accessor' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'FedImportServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

0

All Articles