Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method call counts persist for entire testMethod process #50

Open
kvizcarra opened this issue Sep 5, 2017 · 2 comments
Open

Method call counts persist for entire testMethod process #50

kvizcarra opened this issue Sep 5, 2017 · 2 comments

Comments

@kvizcarra
Copy link

Inside a testMethod, we loop through a list of TestCases, stub dependencies, and verify behavior. The problem we've encountered is the verify call will fail on the second test case instance saying that the method was expected to be called once but actually was called twice. Instantiating ApexMocks and the mock dependencies inside the loop doesn't work. What does work is if we pass unique inputs to the mock method we are verifying for each test case instance.

Example

static testMethod void getUsersTest() {
    for (TestCase testCase : getTestCases()) {
        fflib_ApexMocks mocks = new fflib_ApexMocks();

        IUserRepository mockUserRepository = (IUserRepository)mocks.mock(IUserRepository.class);

        [...]
        ((IUserRepository) mocks.verify(mockUserRepository, 1)).getUsers(testCase.nameFilter);
        [...]
    }
}
 
private class TestCase {
    string nameFilter;
    integer someOtherFilter;
 
    TestCase(string nameFilter, integer someOtherFilter) {
        this.nameFilter = nameFilter;
        this.someOtherFilter = someOtherFilter;
    }
}

These test cases would fail:

private static List<TestCase> getTestCases() {
    return new List<TestCase>{
        new TestCase('Bob', null),
        new TestCase('Bob', 0),
        new TestCase('Bob', 5),
    };
}

And these would succeed:

private static List<TestCase> getTestCases() {
    return new List<TestCase>{
        new TestCase('Bob', null),
        new TestCase('Bib', 0),
        new TestCase('Berb', 5),
    };
}

Possible cause

After some digging into the library, I may have found what's causing this. It looks like the library is using fflib_MethodCountRecorder.methodArgumentsByTypeName to keep track of the method calls, but since it's static it persists for the entire testMethod process.

@dfruddffdc
Copy link
Contributor

This is similar to the issue raised in #29. As a workaround, you can stub instances of objects independently using ApexMocksConfig.HasIndependentMocks.

See #42 for examples of its usage.

Marked as an enhancement request.

@kvizcarra
Copy link
Author

Confirmed the workaround works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants