apex-enterprise-patterns / fflib-apex-mocks

An Apex mocking framework for true unit testing in Salesforce, with Stub API support
BSD 3-Clause "New" or "Revised" License
422 stars 214 forks source link

Test_ApexMocksTest failure in namespaced packager orgs #14

Closed tfuda closed 8 years ago

tfuda commented 8 years ago

We recently had to deploy the fflib-apex-mocks classes into one of our namespaced package development orgs because we updated to the latest fflib-apex-common classes, and that introduced a dependency on fflib-apex-mocks. When I run the unit tests for the fflib-apex-mocks classes, I get the following error from Test_ApexMocksTest:

Error message: System.AssertException: Assertion Failed: Expected: Invalid conversion from runtime type fflib_ApplicationTest.ContactsConstructor to fflib_SObjectDomain.IConstructable, Actual: Invalid conversion from runtime type PatronTicket.fflib_ApplicationTest.ContactsConstructor to PatronTicket.fflib_SObjectDomain.IConstructable

Stack trace: Class.PatronTicket.fflib_ApplicationTest.callingDomainFactoryWithContructorClassThatDoesNotSupportIConstructableShouldGiveException: line 191, column 1

There are two assertions in this test method that are looking for exact string matches on the exception message, but when run in a namespaced org, the exception message doesn't match because the fflib classes are prefixed with the package namespace. I made the following change to use a regular expression that is tolerant of the package namespace prefix in order to check the exception messages:

    @IsTest
    private static void callingDomainFactoryWithContructorClassThatDoesNotSupportIConstructableShouldGiveException()
    {
        try {
            Domain.newInstance(new List<Contact>{ new Contact(LastName = 'TestContactLName') });
            System.assert(false, 'Expected exception');
        } catch (System.TypeException e) {
            System.assert(Pattern.Matches('Invalid conversion from runtime type \\w*\\.?fflib_ApplicationTest\\.ContactsConstructor to \\w*\\.?fflib_SObjectDomain\\.IConstructable',
                e.getMessage()), 'Exception message did not match the expected pattern: ' + e.getMessage());
//          System.assertEquals('Invalid conversion from runtime type fflib_ApplicationTest.ContactsConstructor to fflib_SObjectDomain.IConstructable', e.getMessage());
        }   

        try {
            Domain.newInstance(new List<SObject>{ new Contact(LastName = 'TestContactLName') }, Contact.SObjectType);
            System.assert(false, 'Expected exception');
        } catch (System.TypeException e) {
            System.assert(Pattern.Matches('Invalid conversion from runtime type \\w*\\.?fflib_ApplicationTest\\.ContactsConstructor to \\w*\\.?fflib_SObjectDomain\\.IConstructable2',
                e.getMessage()), 'Exception message did not match the expected pattern: ' + e.getMessage());
//          System.assertEquals('Invalid conversion from runtime type fflib_ApplicationTest.ContactsConstructor to fflib_SObjectDomain.IConstructable2', e.getMessage());
        }       
    }   

I can submit a pull request for this if you like.

dfruddffdc commented 8 years ago

Hi, I'm kind of surprised this issue hasn't been reported before, but the fix seems solid. I'd very much enjoy a pull request, and I'm happy to review.

tfuda commented 8 years ago

I just realized I opened this issue in the wrong project. This is an fflib-apex-common test class... Test_ApplicationTest. Sorry. I'll submit a pull request to fflib-apex-common.

dfruddffdc commented 8 years ago

Ah yes, of course! I'll see you over in fflib-apex-common for that pull request :)