I'm unit testing a file I wrote, and there's a particular service that I'm mocking out which is turning out to be quite problematic.
This should be minimum readable code
EmailSpec.groovy
void "throwaway test name"()
{
given:
def problematicServiceMock = mockFor(ProblematicService)
//Used for configurations. In this case, there are no parameters since
//individual parameters should be null safe. Might need to initialize
//params for problematicService to false to avoid null issues.
def params = new GrailsParameterMap([:], null)
//in test code, this has values and is a shared item, but none of those
//should be important here
def user = new User()
//Simple return just to imitate functionality
//If I change arguments list to match arguments from source service, get
//complaint about no arguments matching emptylist, User, null, null, null
//The below format worked in another test
problematicServiceMock.demand.sampleFunction (0..1) {List departments,
User user ->
Set<String> sampleOutput = ["sample output"]
sampleOutput
}
service.problematicService = problematicServiceMock.createMock()
when:
def myMap = service.getMyMap(params, user)
Set results = myMap.getEmailList()
then:
results.size() == 1
println(results) //manual checking in place of assert temporarily
}
EmailService.groovy
//...
Map getMyMap(GrailsParameterMap params, User user) {
//Bunch of irrelevent processing code to populate map
Map myMapData = [:]
myMapData.emails = [
//Bunch of other services that are retrieving fine
//Sorry for arm-length arguments
sampleMap: problematicService.sampleFunction(
params.list('filler'),
user,
params.boolean('filler2'),
params.boolean('filler3'),
params.boolean('filler4'))
myMapData.getEmailList = { ->
Set<String> emailTo = [] as Set
myMapData.emails.each { k, v -> //this line previously had String k, Set v, but
// whined about converting closure to set.
//Definitely a hint to the problem
emailTo.addAll(v)
}
emailTo
}
myMapData
}
problematicService.groovy
class ProblematicService {
//irrelevant
Set<String> sampleFunction(List<String> listArg, User user, boolean boolArg1, boolean boolArg2, boolean boolArg3) {
//irrelevant since it's being mocked out
}
}
Instead of outputting [sample output], I get a reference to the closure's anonymous identity. myPackage.EmailSpec$_$spock_feature_0_5_closure7@6c9cd7aa This obviously isn't correct. There are three other services that are returning just fine, but they don't have the primitive arguments and only consist of a List (shows empty in testing) and user. Not sure what to do here. The problem is apparent, but not the solution.
Aucun commentaire:
Enregistrer un commentaire