/* * Verifies a method was invoked the expected number of times, with the expected arguments. * @param qualifiedMethod The method to be verified. * @param methodArg The arguments of the method that needs to be verified. * @param verificationMode The verification mode that holds the setting about how the verification should be performed. */ protected override void verify(fflib_QualifiedMethod qm, fflib_MethodArgValues methodArg, fflib_VerificationMode verificationMode) { int methodCount = getMethodCount(qm, methodArg); string qualifier = ""; int expectedCount = null; if ((verificationMode.VerifyMin == verificationMode.VerifyMax) && methodCount != verificationMode.VerifyMin) { expectedCount = verificationMode.VerifyMin; } else if (verificationMode.VerifyMin != null && verificationMode.VerifyMin > methodCount) { expectedCount = verificationMode.VerifyMin; qualifier = " or more times"; } else if (verificationMode.VerifyMax != null && verificationMode.VerifyMax < methodCount) { expectedCount = verificationMode.VerifyMax; qualifier = " or fewer times"; } if (expectedCount != null) { throwException(qm, "", expectedCount, qualifier, methodCount, verificationMode.CustomAssertMessage); } }
private int getMethodCount(fflib_QualifiedMethod qm, fflib_MethodArgValues methodArg) { List <fflib_IMatcher> matchers = fflib_Match.Matching ? fflib_Match.getAndClearMatchers(methodArg.argValues.size()): null; int retval = 0; List <fflib_MethodArgValues> methodArgs = fflib_MethodCountRecorder.getMethodArgumentsByTypeName().get(qm); if (methodArgs != null) { if (matchers != null) { foreach (fflib_MethodArgValues args in methodArgs) { if (fflib_Match.matchesAllArgs(args, matchers)) { capture(matchers); retval++; } } } else { return(countCalls(methodArgs, methodArg)); } } return(retval); }
private int countCalls(List <fflib_MethodArgValues> methodArgs, fflib_MethodArgValues methodArg) { int count = 0; foreach (fflib_MethodArgValues arg in methodArgs) { if (arg == methodArg) { count++; } } return(count); }
/** * Mock a non-void method. Called by generated mock instance classes, not directly by a developers * code. * @param mockInstance The mock object instance. * @param methodName The method for which to prepare a return value. * @param methodArgTypes The method argument types for which to prepare a return value. * @param methodArgValues The method argument values for which to prepare a return value. */ public object mockNonVoidMethod(object mockInstance, string methodName, List <Type> methodArgTypes, List <object> methodArgValues) { fflib_QualifiedMethod qm = new fflib_QualifiedMethod(extractTypeName(mockInstance), methodName, methodArgTypes, mockInstance); fflib_MethodArgValues argValues = new fflib_MethodArgValues(methodArgValues); fflib_InvocationOnMock invocation = new fflib_InvocationOnMock(qm, argValues, mockInstance); if (Verifying) { verifyMethodCall(invocation); } else if (Stubbing) { fflib_MethodReturnValue methotReturnValue = prepareMethodReturnValue(invocation); if (DoThrowWhenExceptions != null) { methotReturnValue.thenThrowMulti(DoThrowWhenExceptions); DoThrowWhenExceptions = null; return(null); } if (this.myAnswer != null) { methotReturnValue.thenAnswer(this.myAnswer); this.myAnswer = null; return(null); } return(null); } else { recordMethod(invocation); return(returnValue(invocation)); } return(null); }