UnitTesting and ArcObjects 10

3497
14
08-31-2011 01:34 AM
YvoWeidmann
New Contributor II
Dear List

I am trying to use UnitTests with the provided framework within VisualStudio 2010 and ArcGIS (ArcEditor) with C#.

The main problem seems the initializing of the ArcObjects. As soon as I am using an ArcObject object in a test method the following exception is thrown:
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

As first I initialize the ArcObjects environment with a test method defined as [AssemblyInitialize]:
-----------------------------
private static AoInitialize aoInitialize;
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context) {
RuntimeManager.BindLicense(ProductCode.EngineOrDesktop);
aoInitialize = new AoInitializeClass();
aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor);
}
-----------------------------

The license is correctly checked out. But as mentioned, ArcObjects-based objects are throwing the InvalidComObjectException.

I tried several ways of initializing, but till now unsuccessfully.

Does anyone has an idea? I am looking for any hint.

Best regards,
Yvo
0 Kudos
14 Replies
LukeBadgerow
New Contributor
I don't believe that this is related to the STA/MTA portion of this issue

(sorry for the bump, but this looks like the most directly related Unit Testing thread across the forums)

I'm getting hit by the following error every time I run into anything that initially sits in the ESRI.ArcGIS.esriSystem package (specifically UID/UIDClass and IPropertySet).  This will happen regardless of whether or not I attempt to access through a mocking framework (I'm currently using Rhino Mocks):


Warning: Test Run deployment issue:  The assembly or module 'ESRI.DADF.Core' directly or indirectly referenced by the container 'C:\[MyArcGISAddinDeploymentDirectory]\editorassisttests.dll' was not found.


I have been working through the process of refactoring as much of my code to NOT use AO as much as possible, in the expectation that I could reach a target of around 80% code coverage (unit/integration test combo). But getting IEditor and IFeatureWorkspace are kind of tough to avoid.  If anyone has any guidance, I'd gladly accept all the help I can get.  I'm fairly new to Unit Testing, and trying to learn it in the wild by translating java examples across to AO is a little bit of a brain drain.  Below are a couple code examples of simple situations that I'm using to just verify that I can get these "things".

        [TestMethod()]
        public void CheckLayoutView_ReturnsIFeatureWorkspaceType () {
            MockRepository mockrepo = new MockRepository();

            IPlotDocument plotter = mockrepo.DynamicMock<PlotDocumentGeneratorClass>();
            IFeatureWorkspace transworkspace = plotter.SetCurrentWorkspace();
            Assert.IsInstanceOfType(transworkspace, typeof(ESRI.ArcGIS.Geodatabase.IFeatureWorkspace));
        }
        [TestMethod()]
        public void GetEditor_MockingTest () {
            MockRepository mockrepo = new MockRepository();
            PlotDocumentGeneratorClass plotter = new PlotDocumentGeneratorClass();
            UID test = mockrepo.StrictMock<UID>();
            Assert.IsNotNull(test);

        }
        [TestMethod()]
        public void GetWorkspace_ReturnsNotNull () {
            PlotDocumentGeneratorClass plotter = new PlotDocumentGeneratorClass();
            IFeatureWorkspace ws = plotter.GetWorkspace("LBADGEROW", "C:\\", false);
            Assert.IsNotNull(ws);
        }
0 Kudos
RichardWatson
Frequent Contributor
I don't have 10.0 installed at the moment.

Where is ESRI.DADF.Core on your system?

Suggest that you run fuslogvw to see why the references assembly was not found.

How does your project reference this assembly?
0 Kudos
LukeBadgerow
New Contributor
Thanks for the quick response!  Here is where I'm at now:

I was also receiving a warning that I was neglecting to address (I've currently also got a separate issue where the addin cannot access our SOA upon deployment) that was suggesting that I switch the Embed Interop Types property to false on the AO packages for the [testing] project.  Once I did that I was able to at least create mock AO instances (UIDClass, IPropertySet2, IApplication).  With a little bit of refactoring I am able to pass the Mock instances of UIDClass and IApplication into the method that I'm attempting to test, but I'm currently working through setting the "esriEditor.Editor" Value to my mock UIDClass correctly

        public IEditor GetEditor (UID uid, IApplication app) {
            IEditor editor = null;
            editor = app.FindExtensionByCLSID(uid) as IEditor;
            return editor;
        }

        [TestMethod()]
        public void GetEditor_MockingTest () {
            MockRepository mockrepo = new MockRepository();
            PlotDocumentGeneratorClass plotter = new PlotDocumentGeneratorClass();

            UID testuid = mockrepo.StrictMockWithRemoting<UIDClass>();
            testuid.Value = "esriEditor.Editor";
            IApplication testapp = mockrepo.StrictMock<IApplication>();
            IEditor testeditor = plotter.GetEditor(testuid, testapp);

            Assert.IsNotNull(testeditor);
        }
0 Kudos
MannusEtten
New Contributor
which mocking-framework do you use Luke?

So when I do AOInitialize at classinitiliaze i am rid of my problems?
0 Kudos
LukeBadgerow
New Contributor
which mocking-framework do you use Luke?

So when I do AOInitialize at classinitiliaze i am rid of my problems?



Apologies for the long delay in responding.  Currently I'm using Rhino Mocks, but as I expand my unit/integration tests for this group of projects, I'm starting to run into a few issues (Namely with building my MSTest project on a deployment server) and am starting to contemplate switching all of that architecture up.

I haven't run AOInitialize as yet.  I have previously been attempting to keep as much AO logic from bleeding into my testing work as possible.  Considering I'm trying to test business logic, and am often disconnected from my license server, it's beneficial for me to isolate out the AO as much as possible.  I will fire up some tests with it though and get back with you.
0 Kudos