Last active
August 29, 2015 14:24
-
-
Save pjakubczyk/a247156e15064d322c91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def preferencesStoreMock | |
def "setup"() { | |
preferencesStoreMock = Mock(PreferencesStore) | |
def module = generateModule(preferencesStoreMock) as TestModule | |
DaggerInjector.start(module); | |
} | |
// generate a DaggerModule based on collection of mocks | |
def generateModule(Object... mocks) { | |
// Very useful util class! | |
def mockDetector = new MockDetector() | |
// Must use DaggerModule class. | |
// Using TestModule class resulted with strange looking method names | |
def daggerMethods = DaggerModule.getMethods() | |
mocks.collectEntries { mock -> | |
// Getting only one element. | |
// Dagger is based on provider's return types. By design there is one method returning | |
// a type. | |
def providerMethodName = daggerMethods.find { | |
it.returnType == (mockDetector.isMock(mock) ? | |
mockDetector.asMock(mock).type : mock.class) | |
}.getName() | |
// It's very important to override the method parameters. Giving empty param list | |
// just adds a new method with no arguments. Object... is an alias for any argument number. | |
def providerAsClosure = { Object... objects -> | |
mock | |
} | |
// return a map entry | |
[providerMethodName, providerAsClosure] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment