Question

I'm new to PEX and Moles just wondering how to UnitTest with moles something like below.

I just need a UniDynArray to test. But Creating a UniDynArray depends on the UniSession and UniSession Depends on UniObjects.OpenConnection. When i run this code i get error from ThirdParty dll saying sessions aren't open

Please help

[TestMethod, HostType("Moles")]
    public void Constructor2WithMoles()
    {
        using (MolesContext.Create())
        {
            //Should I make the Third party session like this ???
            MUniSession msession = new MUniSession();

            //Here What Actually Happens in the code is uniObject opensession return session
            // UniSession session = UniObjects.OpenSession(a,b,c,d); How should i do this
           //???? MUniObjects.OpenSessionStringStringStringString = (a, b, c, d) => msession;

            MUniDynArray mdata = new MUniDynArray();

            mdata.InsertInt32Int32String = (column, index, strValue) =>
                                               {
                                                   column = 1;
                                                   index = 1;
                                                   strValue = "Personal Auto";
                                               };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
                                               {
                                                   column = 2;
                                                   index = 1;
                                                   strValue = "1.1";
                                               };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
            {
                column = 3;
                index = 1;
                strValue = "05/05/2005";
            };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
            {
                column = 4;
                index = 1;
                strValue = "Some Description";
            };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
            {
                column = 5;
                index = 1;
                strValue = "20";
            };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
            {
                column = 6;
                index = 1;
                strValue = "1";
            };

            History target = new History(mdata, 1);

            Assert.AreEqual<string>("Some Description", target.Description);
        }
        // TODO: CREATE Mole asserts
    }
Was it helpful?

Solution

First, I strongly recommend waiting to use Fakes Stub and Shim types, in .NET 4.5 and Visual Studio 2012 (releasing 12 SEP 2012). Fakes is the productized version of Moles. To start using Fakes, download the free Ultimate version of VS2012 Release Candidate.

To answer your question, you should isolate your 3rd party dependency, using the Dependency Injection design pattern. Many users of Shim/Mole types overlook implementing the Dependency Injection pattern -- use Shim/Mole types, only when no other options remain. I assume the 3rd party library exposes interfaces. Fakes/Moles automatically converts interfaces to Stub types that can be used for tests. You'll need to create concrete stubs from the interfaces, for the production code to use. These stubs are simply a wrapper for the targeted 3rd party library type. Look up any article on Dependency Injection, for details on how to implement the pattern -- this pattern is quick and easy to implement, especially when using a refactoring tool, such as Resharper or Refactor!Pro.

Stub type method calls are detoured, using the same lambda/delegate syntax as Shim/Mole types. Stub types are good for generating stubs unique to a single or small number of tests. Otherwise, creating a concrete test stub for use in several tests is the best option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top