How to use Assert method of Telerik.JustMock.Core.Behaviors.InvocationOccurrenceBehavior class

Best JustMockLite code snippet using Telerik.JustMock.Core.Behaviors.InvocationOccurrenceBehavior.Assert

MocksRepository.cs

Source:MocksRepository.cs Github

copy

Full Screen

...341 return;342 }343 invocation.InArrange = this.sharedContext.InArrange;344 invocation.InArrangeArgMatching = this.sharedContext.InArrangeArgMatching;345 invocation.InAssertSet = this.sharedContext.InAssertSet;346 invocation.InRunClassConstructor = this.sharedContext.RunClassConstructorCount > 0;347 invocation.Recording = this.Recorder != null;348 invocation.RetainBehaviorDuringRecording = this.sharedContext.DispatchToMethodMocks;349 invocation.Repository = this;350 bool methodMockProcessed = false;351 if (invocation.Recording)352 {353 this.Recorder.Record(invocation);354 }355 if (!invocation.Recording || invocation.RetainBehaviorDuringRecording)356 {357 methodMockProcessed = DispatchInvocationToMethodMocks(invocation);358 }359 invocation.ThrowExceptionIfNecessary();360 if (!methodMockProcessed)361 {362 // We have to be careful for the potential exception throwing in the assertion context,363 // so skip CallOriginalBehavior processing364 var mock = invocation.MockMixin;365 if (mock != null)366 {367 var fallbackBehaviorsToExecute =368 mock.FallbackBehaviors369 .Where(behavior => !invocation.InAssertSet || !(behavior is CallOriginalBehavior))370 .ToList();371 foreach (var fallbackBehavior in fallbackBehaviorsToExecute)372 {373 fallbackBehavior.Process(invocation);374 }375 }376 else377 {378 invocation.CallOriginal = CallOriginalBehavior.ShouldCallOriginal(invocation) && !invocation.InAssertSet;379 }380 }381 if (!invocation.CallOriginal && !invocation.IsReturnValueSet && invocation.Method.GetReturnType() != typeof(void))382 {383 Type returnType = invocation.Method.GetReturnType();384 object defaultValue = null;385#if !PORTABLE386 if (returnType.BaseType != null && returnType.BaseType == typeof(Task))387 {388 Type taskGenericArgument = returnType.GenericTypeArguments.FirstOrDefault();389 object taskArgumentDefaultValue = taskGenericArgument.GetDefaultValue();390 // create a task with default value to return, by using the casting help method in MockingUtil391 MethodInfo castMethod = typeof(MockingUtil).GetMethod("TaskFromObject", BindingFlags.Static | BindingFlags.Public);392 MethodInfo castMethodGeneric = castMethod.MakeGenericMethod(taskGenericArgument);393 defaultValue = castMethodGeneric.Invoke(null, new object[] { taskArgumentDefaultValue });394 }395 else396#endif397 {398 defaultValue = returnType.GetDefaultValue();399 }400 invocation.ReturnValue = defaultValue;401 }402#if !PORTABLE403 try404 {405 if (MockingContext.Plugins.Exists<IDebugWindowPlugin>())406 {407 Func<object, Type, ObjectInfo> CreateObjectInfo = (value, type) =>408 {409 ObjectInfo resultInfo = (value != null) ? ObjectInfo.FromObject(value) : ObjectInfo.FromNullObject(type);410 return resultInfo;411 };412 var debugWindowPlugin = MockingContext.Plugins.Get<IDebugWindowPlugin>();413 var mockInfo = MockInfo.FromMethodBase(invocation.Method);414 ObjectInfo[] invocationsArgInfos =415 invocation.Args.Select(416 (arg, index) =>417 {418 var argInfo = CreateObjectInfo(arg, invocation.Method.GetParameters()[index].ParameterType);419 return argInfo;420 }).ToArray();421 ObjectInfo invocationInstanceInfo = ObjectInfo.FromObject(invocation.Instance ?? invocation.Method.DeclaringType);422 ObjectInfo invocationResultInfo = CreateObjectInfo(invocation.ReturnValue, invocation.Method.GetReturnType());423 var invocationInfo = new InvocationInfo(invocationInstanceInfo, invocationsArgInfos, invocationResultInfo);424 debugWindowPlugin.MockInvoked(425 invocation.Repository.RepositoryId,426 invocation.Repository.GetRepositoryPath(),427 mockInfo,428 invocationInfo);429 }430 }431 catch (Exception e)432 {433 System.Diagnostics.Trace.WriteLine("Exception thrown calling IDebugWindowPlugin plugin: " + e);434 }435#endif436 }437 internal T GetValue<T>(object owner, object key, T dflt)438 {439 object value;440 if (valueStore.TryGetValue(new KeyValuePair<object, object>(owner, key), out value))441 {442 return (T)value;443 }444 else445 {446 return dflt;447 }448 }449 internal void StoreValue<T>(object owner, object key, T value)450 {451 valueStore[new KeyValuePair<object, object>(owner, key)] = value;452 }453 internal IDisposable StartRecording(IRecorder recorder, bool dispatchToMethodMocks)454 {455 return this.sharedContext.StartRecording(recorder, dispatchToMethodMocks);456 }457 internal IDisposable StartArrangeArgMatching()458 {459 return this.sharedContext.StartArrangeArgMatching();460 }461 internal void AddMatcherInContext(IMatcher matcher)462 {463 if (!this.sharedContext.InArrange || this.sharedContext.Recorder != null)464 {465 this.MatchersInContext.Add(matcher);466 }467 }468 internal object Create(Type type, MockCreationSettings settings)469 {470 object delegateResult;471 if (TryCreateDelegate(type, settings, out delegateResult))472 {473 return delegateResult;474 }475 bool isSafeMock = settings.FallbackBehaviors.OfType<CallOriginalBehavior>().Any();476 this.CheckIfCanMock(type, !isSafeMock);477 this.EnableInterception(type);478 bool canCreateProxy = !type.IsSealed;479 MockMixin mockMixinImpl = this.CreateMockMixin(type, settings);480 ConstructorInfo[] ctors = type.GetConstructors();481 bool isCoclass = ctors.Any(ctor => ctor.IsExtern());482 bool hasAdditionalInterfaces = settings.AdditionalMockedInterfaces != null && settings.AdditionalMockedInterfaces.Length > 0;483 bool hasAdditionalProxyTypeAttributes = settings.AdditionalProxyTypeAttributes != null && settings.AdditionalProxyTypeAttributes.Any();484 bool shouldCreateProxy = settings.MustCreateProxy485 || hasAdditionalInterfaces486 || isCoclass487 || type.IsAbstract || type.IsInterface488 || !settings.MockConstructorCall489 || hasAdditionalProxyTypeAttributes490 || !ProfilerInterceptor.IsProfilerAttached491 || !ProfilerInterceptor.TypeSupportsInstrumentation(type);492 bool createTransparentProxy = MockingProxy.CanCreate(type) && !ProfilerInterceptor.IsProfilerAttached;493 Exception proxyFailure = null;494 object instance = null;495 if (canCreateProxy && shouldCreateProxy)496 {497 try498 {499 instance = mockFactory.Create(type, this, mockMixinImpl, settings, createTransparentProxy);500 }501 catch (ProxyFailureException ex)502 {503 proxyFailure = ex.InnerException;504 }505 }506 IMockMixin mockMixin = instance as IMockMixin;507 if (instance == null)508 {509 if (type.IsInterface || type.IsAbstract)510 throw new MockException(String.Format("Abstract type '{0}' is not accessible for inheritance.", type));511 if (hasAdditionalInterfaces)512 throw new MockException(String.Format("Type '{0}' is not accessible for inheritance. Cannot create mock object implementing the specified additional interfaces.", type));513 if (!ProfilerInterceptor.IsProfilerAttached && !createTransparentProxy)514 ProfilerInterceptor.ThrowElevatedMockingException(type);515 if (settings.MockConstructorCall && type.IsValueType)516 {517 settings.MockConstructorCall = false;518 settings.Args = null;519 }520 if (!settings.MockConstructorCall)521 {522 try523 {524 instance = type.CreateObject(settings.Args);525 }526 catch (MissingMethodException)527 {528 settings.MockConstructorCall = true;529 }530 }531 if (settings.MockConstructorCall)532 {533 instance = MockingUtil.GetUninitializedObject(type);534 }535 if (!createTransparentProxy)536 {537 mockMixin = this.CreateExternalMockMixin(type, instance, settings);538 }539 }540 else541 {542 this.controlledMocks.Add(new WeakReference(instance));543 }544 if (type.IsClass)545 GC.SuppressFinalize(instance);546 if (createTransparentProxy)547 {548 if (mockMixin == null)549 {550 mockMixin = mockMixinImpl;551 }552 instance = MockingProxy.CreateProxy(instance, this, mockMixin);553 }554 mockMixin.IsInstanceConstructorMocked = settings.MockConstructorCall;555 return instance;556 }557 internal IMockMixin CreateExternalMockMixin(Type mockObjectType, object mockObject, MockCreationSettings settings)558 {559 if (mockObjectType == null)560 {561 if (mockObject == null)562 throw new ArgumentNullException("mockObject");563 mockObjectType = mockObject.GetType();564 }565 this.EnableInterception(mockObjectType);566 if (mockObject == null)567 throw new MockException(String.Format("Failed to create instance of type '{0}'", mockObjectType));568 MockMixin mockMixin = this.CreateMockMixin(mockObjectType, settings, false);569 IMockMixin compoundMockMixin = mockFactory.CreateExternalMockMixin(mockMixin, settings.Mixins);570 lock (externalMixinDatabase)571 {572 if (mockObjectType.IsValueType())573 {574 externalMixinDatabase.RemoveAll(kvp => kvp.Key.Equals(mockObject));575 externalMixinDatabase.Add(new KeyValuePair<object, IMockMixin>(mockObject, compoundMockMixin));576 }577 else578 {579 externalReferenceMixinDatabase.Add(mockObject, compoundMockMixin);580 }581 compoundMockMixin.ExternalizedMock = mockObject;582 this.controlledMocks.Add(new WeakReference(compoundMockMixin));583 }584 return compoundMockMixin;585 }586 internal ProxyTypeInfo CreateClassProxyType(Type classToProxy, MockCreationSettings settings)587 {588 MockMixin mockMixinImpl = this.CreateMockMixin(classToProxy, settings);589 return mockFactory.CreateClassProxyType(classToProxy, this, settings, mockMixinImpl);590 }591 private void CheckIfCanMock(Type type, bool checkSafety)592 {593 if (KnownUnmockableTypes.Contains(type)594 || typeof(Delegate).IsAssignableFrom(type))595 throw new MockException("Cannot create mock for type due to CLR limitations.");596 if (checkSafety)597 ProfilerInterceptor.CheckIfSafeToInterceptWholesale(type);598 }599 internal void InterceptStatics(Type type, MockCreationSettings settings, bool mockStaticConstructor)600 {601 if (!ProfilerInterceptor.IsProfilerAttached)602 ProfilerInterceptor.ThrowElevatedMockingException(type);603 if (!settings.FallbackBehaviors.OfType<CallOriginalBehavior>().Any())604 ProfilerInterceptor.CheckIfSafeToInterceptWholesale(type);605 var mockMixin = (IMockMixin)Create(typeof(ExternalMockMixin),606 new MockCreationSettings607 {608 Mixins = settings.Mixins,609 SupplementaryBehaviors = settings.SupplementaryBehaviors,610 FallbackBehaviors = settings.FallbackBehaviors,611 MustCreateProxy = true,612 });613 mockMixin.IsStaticConstructorMocked = mockStaticConstructor;614 lock (staticMixinDatabase)615 staticMixinDatabase[type] = mockMixin;616 this.EnableInterception(type);617 }618 private MockMixin CreateMockMixin(Type declaringType, MockCreationSettings settings)619 {620 return CreateMockMixin(declaringType, settings, settings.MockConstructorCall);621 }622 private MockMixin CreateMockMixin(Type declaringType, MockCreationSettings settings, bool mockConstructorCall)623 {624 var mockMixin = new MockMixin625 {626 Repository = this,627 DeclaringType = declaringType,628 IsInstanceConstructorMocked = mockConstructorCall,629 };630 foreach (var behavior in settings.SupplementaryBehaviors)631 mockMixin.SupplementaryBehaviors.Add(behavior);632 foreach (var behavior in settings.FallbackBehaviors)633 mockMixin.FallbackBehaviors.Add(behavior);634 return mockMixin;635 }636 [ArrangeMethod]637 internal TMethodMock Arrange<TMethodMock>(Expression expression, Func<TMethodMock> methodMockFactory)638 where TMethodMock : IMethodMock639 {640 TMethodMock result = methodMockFactory();641 using (this.sharedContext.StartArrange())642 {643 result.Repository = this;644 result.ArrangementExpression = ExpressionUtil.ConvertMockExpressionToString(expression);645 result.CallPattern = CallPatternCreator.FromExpression(this, expression);646 AddArrange(result);647 }648#if !PORTABLE649 var createInstanceLambda = ActivatorCreateInstanceTBehavior.TryCreateArrangementExpression(result.CallPattern.Method);650 if (createInstanceLambda != null)651 {652 var createInstanceMethodMock = Arrange(createInstanceLambda, methodMockFactory);653 ActivatorCreateInstanceTBehavior.Attach(result, createInstanceMethodMock);654 }655#endif656 return result;657 }658 /// <summary>659 /// Since we are not able to record deffered execution, check whether mock is about to present660 /// IQueryable<> or IEnumerable<>. If it so, then throw an exception to prompt the user to use661 /// Mock.Arrange overloads with expressions.662 /// See also https://stackoverflow.com/questions/3894490/linq-what-is-the-quickest-way-to-find-out-deferred-execution-or-not663 /// </summary>664 /// <typeparam name="TMethodMock">Mock return type</typeparam>665 /// <param name="methodMockFactory">Mock factory</param>666 private void CheckDefferedExecutionTypes<TMethodMock>(Func<TMethodMock> methodMockFactory)667 {668 var mockFactoryType = methodMockFactory.GetType().GetGenericArguments().FirstOrDefault();669 if (mockFactoryType.IsGenericType && mockFactoryType.GetGenericTypeDefinition() == typeof(FuncExpectation<>))670 {671 var mockFactoryGenericArguments = mockFactoryType.GetGenericArguments();672 if (mockFactoryGenericArguments673 .Where(a => a.IsGenericType)674 .Where(a => a.GetGenericTypeDefinition() == typeof(IQueryable<>)675 || a.GetGenericTypeDefinition() == typeof(IEnumerable<>))676 .Any())677 {678 throw new MockException("Cannot arrange action with potential deffered execution, use Mock.Arrange with expressions instead.");679 }680 }681 }682 [ArrangeMethod]683 internal TMethodMock Arrange<TMethodMock>(Action memberAction, Func<TMethodMock> methodMockFactory)684 where TMethodMock : IMethodMock685 {686 using (this.sharedContext.StartArrange())687 {688 var result = methodMockFactory();689 CheckDefferedExecutionTypes(methodMockFactory);690 result.Repository = this;691 result.CallPattern = CallPatternCreator.FromAction(this, memberAction);692 AddArrange(result);693 return result;694 }695 }696 [ArrangeMethod]697 internal TMethodMock Arrange<TMethodMock>(object instance, MethodBase method, object[] arguments, Func<TMethodMock> methodMockFactory)698 where TMethodMock : IMethodMock699 {700 using (this.sharedContext.StartArrange())701 {702 var result = methodMockFactory();703 result.Repository = this;704 result.CallPattern = CallPatternCreator.FromMethodBase(this, instance, method, arguments);705 AddArrange(result);706 return result;707 }708 }709 [ArrangeMethod]710 internal TMethodMock Arrange<TMethodMock>(CallPattern callPattern, Func<TMethodMock> methodMockFactory)711 where TMethodMock : IMethodMock712 {713 using (this.sharedContext.StartArrange())714 {715 var result = methodMockFactory();716 result.Repository = this;717 result.CallPattern = callPattern;718 AddArrange(result);719 return result;720 }721 }722 private void AssertBehaviorsForMocks(IEnumerable<IMethodMock> mocks, bool ignoreMethodMockOccurrences)723 {724 foreach (var methodMock in mocks)725 {726 bool occurrenceAsserted = ignoreMethodMockOccurrences;727 if (!ignoreMethodMockOccurrences728 && !methodMock.OccurencesBehavior.LowerBound.HasValue729 && !methodMock.OccurencesBehavior.UpperBound.HasValue)730 {731 methodMock.OccurencesBehavior.Assert(1, null);732 occurrenceAsserted = true;733 }734 foreach (var behavior in methodMock.Behaviors.OfType<IAssertableBehavior>())735 if (!occurrenceAsserted || behavior != methodMock.OccurencesBehavior)736 behavior.Assert();737 }738 }739 internal void AssertAll(string message, object mock)740 {741 using (MockingContext.BeginFailureAggregation(message))742 {743 var mocks = GetMethodMocksFromObject(mock);744 AssertBehaviorsForMocks(mocks.Select(m => m.MethodMock), false);745 }746 }747 internal void AssertAll(string message)748 {749 using (MockingContext.BeginFailureAggregation(message))750 {751 var mocks = GetAllMethodMocks();752 AssertBehaviorsForMocks(mocks.Select(m => m.MethodMock), false);753 }754 }755 internal void Assert(string message, object mock, Expression expr = null, Args args = null, Occurs occurs = null)756 {757 using (MockingContext.BeginFailureAggregation(message))758 {759 if (expr == null)760 {761 List<IMethodMock> mocks = new List<IMethodMock>();762 mocks.AddRange(GetMethodMocksFromObject(mock).Select(m => m.MethodMock));763 foreach (var methodMock in mocks)764 {765 foreach (var behavior in methodMock.Behaviors.OfType<IAssertableBehavior>())766 behavior.Assert();767 }768 MockingUtil.UnwrapDelegateTarget(ref mock);769 var mockMixin = GetMockMixin(mock, null);770 if (mockMixin != null)771 {772 foreach (var behavior in mockMixin.FallbackBehaviors.OfType<IAssertableBehavior>()773 .Concat(mockMixin.SupplementaryBehaviors.OfType<IAssertableBehavior>()))774 behavior.Assert();775 }776 }777 else778 {779 CallPattern callPattern = CallPatternCreator.FromExpression(this, expr);780 AssertForCallPattern(callPattern, args, occurs);781 }782 }783 }784 internal void AssertAction(string message, Action memberAction, Args args = null, Occurs occurs = null)785 {786 using (MockingContext.BeginFailureAggregation(message))787 {788 CallPattern callPattern = CallPatternCreator.FromAction(this, memberAction);789 AssertForCallPattern(callPattern, args, occurs);790 }791 }792 internal void AssertSetAction(string message, Action memberAction, Args args = null, Occurs occurs = null)793 {794 using (MockingContext.BeginFailureAggregation(message))795 {796 using (this.sharedContext.StartAssertSet())797 {798 CallPattern callPattern = CallPatternCreator.FromAction(this, memberAction, true);799 AssertForCallPattern(callPattern, args, occurs);800 }801 }802 }803 internal void AssertMethodInfo(string message, object instance, MethodBase method, object[] arguments, Occurs occurs)804 {805 using (MockingContext.BeginFailureAggregation(message))806 {807 CallPattern callPattern = CallPatternCreator.FromMethodBase(instance, method, arguments);808 AssertForCallPattern(callPattern, null, occurs);809 }810 }811 internal void AssertIgnoreInstance(string message, Type type, bool ignoreMethodMockOccurrences)812 {813 using (MockingContext.BeginFailureAggregation(message))814 {815 var methodMocks = GetMethodMocksFromObject(null, type);816 AssertBehaviorsForMocks(methodMocks.Select(m => m.MethodMock), ignoreMethodMockOccurrences);817 }818 }819 internal int GetTimesCalled(Expression expression, Args args)820 {821 CallPattern callPattern = CallPatternCreator.FromExpression(this, expression);822 int callsCount;823 CountMethodMockInvocations(callPattern, args, out callsCount);824 return callsCount;825 }826 internal int GetTimesCalledFromAction(Action action, Args args)827 {828 var callPattern = CallPatternCreator.FromAction(this, action);829 int callsCount;830 CountMethodMockInvocations(callPattern, args, out callsCount);831 return callsCount;832 }833 internal int GetTimesCalledFromMethodInfo(object instance, MethodBase method, object[] arguments)834 {835 var callPattern = CallPatternCreator.FromMethodBase(instance, method, arguments);836 int callsCount;837 CountMethodMockInvocations(callPattern, null, out callsCount);838 return callsCount;839 }840 private HashSet<IMethodMock> CountMethodMockInvocations(CallPattern callPattern, Args args, out int callsCount)841 {842 if (callPattern.IsDerivedFromObjectEquals)843 throw new MockException("Cannot assert calls to methods derived from Object.Equals");844 PreserveRefOutValuesBehavior.ReplaceRefOutArgsWithAnyMatcher(callPattern);845 if (args != null)846 {847 if (args.Filter != null)848 {849 if (args.IsIgnored == null)850 {851 args.IsIgnored = true;852 }853 if (!callPattern.Method.IsStatic854 && args.IsInstanceIgnored == null855 && args.Filter.Method.GetParameters().Length == callPattern.Method.GetParameters().Length + 1)856 {857 args.IsInstanceIgnored = true;858 }859 }860 if (args.IsIgnored == true)861 {862 for (int i = 0; i < callPattern.ArgumentMatchers.Count; i++)863 callPattern.ArgumentMatchers[i] = new AnyMatcher();864 }865 if (args.IsInstanceIgnored == true)866 {867 callPattern.InstanceMatcher = new AnyMatcher();868 }869 callPattern.Filter = args.Filter;870 }871 MethodInfoMatcherTreeNode root;872 callsCount = 0;873 var mocks = new HashSet<IMethodMock>();874 var method = callPattern.Method;875 if (invocationTreeRoots.TryGetValue(method, out root))876 {877 var occurences = root.GetOccurences(callPattern);878 callsCount = occurences.Select(x => x.Calls).Sum();879 foreach (var mock in occurences.SelectMany(x => x.Mocks))880 {881 mocks.Add(mock);882 }883 }884 return mocks;885 }886 private void AssertForCallPattern(CallPattern callPattern, Args args, Occurs occurs)887 {888 int callsCount;889 var mocks = CountMethodMockInvocations(callPattern, args, out callsCount);890 if (occurs != null)891 {892 InvocationOccurrenceBehavior.Assert(occurs.LowerBound, occurs.UpperBound, callsCount, null, null);893 }894 if (mocks.Count == 0)895 {896 MethodInfoMatcherTreeNode funcRoot;897 if (arrangementTreeRoots.TryGetValue(callPattern.Method, out funcRoot))898 {899 var arranges = funcRoot.GetAllMethodMocks(callPattern);900 foreach (var arrange in arranges)901 {902 mocks.Add(arrange.MethodMock);903 }904 }905 }906 if (occurs == null && mocks.Count == 0)907 {908 InvocationOccurrenceBehavior.Assert(Occurs.AtLeastOnce().LowerBound, Occurs.AtLeastOnce().UpperBound, callsCount, null, null);909 }910 else911 {912 AssertBehaviorsForMocks(mocks, occurs != null);913 }914 }915 internal MethodBase GetMethodFromCallPattern(CallPattern callPattern)916 {917 var method = callPattern.Method as MethodInfo;918 if (method == null || !method.IsVirtual)919 return callPattern.Method;920 method = method.NormalizeComInterfaceMethod();921 var valueMatcher = callPattern.InstanceMatcher as IValueMatcher;922 if (valueMatcher != null && valueMatcher.Value != null)923 {924 var valueType = valueMatcher.Value.GetType();925 var mockMixin = GetMockMixin(valueMatcher.Value, null);926 var type = mockMixin != null ? mockMixin.DeclaringType : valueType;927 if (!type.IsInterface && method.DeclaringType.IsAssignableFrom(type))928 {929 var concreteMethod = MockingUtil.GetConcreteImplementer(method, type);930 if (!concreteMethod.IsInheritable() && !ProfilerInterceptor.IsProfilerAttached)931 {932 var reimplementedInterfaceMethod = (MethodInfo)method.GetInheritanceChain().Last();933 if (reimplementedInterfaceMethod.DeclaringType.IsInterface934 && mockFactory.IsAccessible(reimplementedInterfaceMethod.DeclaringType))935 {936 concreteMethod = reimplementedInterfaceMethod;937 }938 }939 method = concreteMethod;940 }941 }942 if (method.DeclaringType != method.ReflectedType)943 method = (MethodInfo)MethodBase.GetMethodFromHandle(method.MethodHandle, method.DeclaringType.TypeHandle);944 return method;945 }946 /// <summary>947 /// Converts the given object to a matcher as follows. This method is most useful for948 /// creating a matcher out of an argument expression.949 /// 950 /// It works as follows:951 /// If the object is not an expression, then a value matcher for that object is returned.952 /// If the object is an expression then:953 /// * if the top of the expression is a method call expression and the member954 /// has the ArgMatcherAttribute then the specific matcher type is instantiaded955 /// with the parameters passed to the method call expression and returned.956 /// If the matcher type is generic, then it is defined with the type of the expression.957 /// * if the top expression is a member or method call and the member958 /// has the ArgIgnoreAttribute, then a TypeMatcher is returned959 /// * otherwise, the expression is evaluated and a ValueMatcher is returned960 /// </summary>961 /// <param name="argumentObj"></param>962 /// <returns></returns>963 internal static IMatcher CreateMatcherForArgument(object argumentObj)964 {965 var argExpr = argumentObj as Expression;966 if (argExpr == null)967 {968 return new ValueMatcher(argumentObj);969 }970 else971 {972 var argMatcher = TryCreateMatcherFromArgMember(argExpr);973 if (argMatcher != null)974 {975 return argMatcher;976 }977 else978 {979 //no matcher, evaluate the original expression980 var argValue = argExpr.EvaluateExpression();981 return new ValueMatcher(argValue);982 }983 }984 }985 internal static IMatcher TryCreateMatcherFromArgMember(Expression argExpr)986 {987 // The expression may end with a conversion which erases the type of the required matcher.988 // Remove the conversion before working with matchers989 while (argExpr.NodeType == ExpressionType.Convert)990 argExpr = ((UnaryExpression)argExpr).Operand;991 ArgMatcherAttribute argAttribute = null;992 Expression[] matcherArguments = null;993 if (argExpr is MethodCallExpression)994 {995 var methodCall = (MethodCallExpression)argExpr;996 argAttribute = (ArgMatcherAttribute)Attribute.GetCustomAttribute(methodCall.Method, typeof(ArgMatcherAttribute));997 matcherArguments = methodCall.Arguments.ToArray();998 }999 else if (argExpr is MemberExpression)1000 {1001 var memberExpr = (MemberExpression)argExpr;1002 argAttribute = (ArgMatcherAttribute)Attribute.GetCustomAttribute(memberExpr.Member, typeof(ArgMatcherAttribute));1003 }1004 if (argAttribute != null)1005 {1006 if (argAttribute.GetType() == typeof(ArgIgnoreAttribute))1007 {1008 return new TypeMatcher(argExpr.Type);1009 }1010 else if (argAttribute.GetType() == typeof(ArgIgnoreTypeAttribute))1011 {1012 var func = Expression.Lambda(argExpr).Compile();1013 var funcResult = func.DynamicInvoke();1014 return new TypeMatcher(funcResult.GetType());1015 }1016 else if (argAttribute.GetType() == typeof(RefArgAttribute) || argAttribute.GetType() == typeof(OutArgAttribute))1017 {1018 var asMemberExpr = argExpr as MemberExpression;1019 if (asMemberExpr != null)1020 {1021 argExpr = asMemberExpr.Expression;1022 }1023 var refCall = (MethodCallExpression)argExpr;1024 var actualArg = refCall.Arguments[0];1025 var memberExpr = actualArg as MemberExpression;1026 if (memberExpr != null && typeof(Expression).IsAssignableFrom(memberExpr.Type) && memberExpr.Expression.Type.DeclaringType == typeof(ArgExpr))1027 {1028 actualArg = (Expression)actualArg.EvaluateExpression();1029 }1030 var argMatcher = CreateMatcherForArgument(actualArg);1031 argMatcher.ProtectRefOut = argAttribute.GetType() == typeof(RefArgAttribute);1032 return argMatcher;1033 }1034 else1035 {1036 var matcherType = argAttribute.Matcher;1037 var matcherArgs = argAttribute.MatcherArgs;1038 if (matcherType.IsGenericTypeDefinition)1039 matcherType = matcherType.MakeGenericType(argExpr.Type);1040 if (matcherArgs == null && matcherArguments != null)1041 matcherArgs = matcherArguments.Select(matcherArgExpr => matcherArgExpr.EvaluateExpression()).ToArray();1042 var matcher = (IMatcher)MockingUtil.CreateInstance(matcherType, matcherArgs);1043 return matcher;1044 }1045 }1046 else1047 {1048 return null;1049 }1050 }1051 private void AddArrange(IMethodMock methodMock)1052 {1053 var method = methodMock.CallPattern.Method;1054 if (methodMock.CallPattern.IsDerivedFromObjectEquals && method.ReflectedType.IsValueType())1055 throw new MockException("Cannot mock Equals method because JustMock depends on it. Also, when Equals is called internally by JustMock, all methods called by it will not be intercepted and will have only their original implementations called.");1056 CheckMethodInterceptorAvailable(methodMock.CallPattern.InstanceMatcher, method);1057 Type declaringType = method.DeclaringType;1058 // If we're arranging a call to a mock object, overwrite its repository.1059 // This will ensure correct behavior when a mock object is arranged1060 // in multiple contexts.1061 var refMatcher = methodMock.CallPattern.InstanceMatcher as ReferenceMatcher;1062 if (refMatcher != null)1063 {1064 var value = refMatcher.Value;1065 var mock = GetMockMixin(value, declaringType);1066 if (mock != null)1067 {1068 methodMock.Mock = mock;1069 mock.Repository = this;1070 }1071 if (value != null)1072 EnableInterception(value.GetType());1073 }1074 EnableInterception(declaringType);1075 PreserveRefOutValuesBehavior.Attach(methodMock);1076 ConstructorMockBehavior.Attach(methodMock);1077 MethodInfoMatcherTreeNode funcRoot;1078 if (!arrangementTreeRoots.TryGetValue(method, out funcRoot))1079 {1080 funcRoot = new MethodInfoMatcherTreeNode(method);1081 arrangementTreeRoots.Add(method, funcRoot);1082 }1083 funcRoot.AddChild(methodMock.CallPattern, methodMock, this.sharedContext.GetNextArrangeId());1084#if !PORTABLE1085 if (ProfilerInterceptor.IsReJitEnabled)1086 {1087 CallPattern.CheckMethodCompatibility(method);1088 CallPattern.CheckInstrumentationAvailability(method);1089 ProfilerInterceptor.RequestReJit(method);1090 }1091 try1092 {1093 if (MockingContext.Plugins.Exists<IDebugWindowPlugin>())1094 {1095 var debugWindowPlugin = MockingContext.Plugins.Get<IDebugWindowPlugin>();1096 var argumentMatchers =1097 methodMock.CallPattern.ArgumentMatchers1098 .SelectMany(1099 (IMatcher matcher, int index) =>1100 {1101 MatcherInfo[] paramsMatchers;1102 var matcherInfo = MatcherInfo.FromMatcherAndParamInfo(matcher, methodMock.CallPattern.Method.GetParameters()[index], out paramsMatchers);1103 if (matcherInfo.Kind == MatcherInfo.MatcherKind.Params && paramsMatchers.Length > 0)1104 {1105 // skip params matcher, but add all contained matchers1106 return paramsMatchers;1107 }1108 return new MatcherInfo[] { matcherInfo };1109 },1110 (IMatcher matcher, MatcherInfo resultMatcherInfo) => resultMatcherInfo)1111 .ToArray();1112 debugWindowPlugin.MockCreated(1113 methodMock.Repository.RepositoryId,1114 methodMock.Repository.GetRepositoryPath(),1115 MockInfo.FromMethodBase(methodMock.CallPattern.Method),1116 argumentMatchers);1117 }1118 }1119 catch (Exception e)1120 {1121 System.Diagnostics.Trace.WriteLine("Exception thrown calling IDebugWindowPlugin plugin: " + e);1122 }1123#endif1124 }1125#if !PORTABLE1126 internal void UpdateMockDebugView(MethodBase method, IMatcher[] argumentMatchers)1127 {1128 try1129 {1130 if (MockingContext.Plugins.Exists<IDebugWindowPlugin>())1131 {1132 var debugWindowPlugin = MockingContext.Plugins.Get<IDebugWindowPlugin>();1133 MatcherInfo[] argumentMatcherInfos = null;1134 if (argumentMatchers != null)1135 {1136 argumentMatcherInfos =1137 argumentMatchers.Select(1138 (IMatcher matcher, int index) =>1139 {1140 MatcherInfo[] dummy;1141 return MatcherInfo.FromMatcherAndParamInfo(matcher, method.GetParameters()[index], out dummy);1142 })1143 .ToArray();1144 }1145 debugWindowPlugin.MockUpdated(1146 this.RepositoryId,1147 this.GetRepositoryPath(),1148 MockInfo.FromMethodBase(method),1149 argumentMatcherInfos);1150 }1151 }1152 catch (Exception e)1153 {1154 System.Diagnostics.Trace.WriteLine("Exception thrown calling IDebugWindowPlugin plugin: " + e);1155 }1156 }1157#endif1158 private void CheckMethodInterceptorAvailable(IMatcher instanceMatcher, MethodBase method)1159 {1160 if (ProfilerInterceptor.IsProfilerAttached)1161 return;1162 var valueMatcher = instanceMatcher as IValueMatcher;1163 if (valueMatcher == null)1164 return;1165 var instance = valueMatcher.Value;1166 if (instance == null)1167 return;1168 if (MockingProxy.CanIntercept(instance, method))1169 return;1170 if (!(instance is IMockMixin) || !method.IsInheritable())1171 ProfilerInterceptor.ThrowElevatedMockingException(method);1172 }1173#if !PORTABLE1174 private void RequestRejitForTypeMethods(Type typeToIntercept)1175 {1176 var methods = new HashSet<MethodBase>();1177 methods.UnionWith(typeToIntercept.GetMethods(MockingUtil.AllMembers));1178 methods.UnionWith(1179 typeToIntercept.GetInheritanceChain()1180 .Where(type => type != typeToIntercept)1181 .SelectMany(type => type.GetMethods(MockingUtil.AllMembers | BindingFlags.DeclaredOnly))1182 .Where(method => !method.IsAbstract && method.DeclaringType != typeof(object)));1183 methods.UnionWith(1184 MockingUtil.GetAllProperties(typeToIntercept)1185 .SelectMany(1186 property =>1187 {1188 var propertyMethods = new List<MethodBase>();1189 var getMethod = property.GetMethod;1190 if (getMethod != null)1191 {1192 propertyMethods.Add(getMethod);1193 }1194 var setMethod = property.SetMethod;1195 if (setMethod != null)1196 {1197 propertyMethods.Add(setMethod);1198 }1199 return propertyMethods;1200 }));1201 foreach (var method in methods)1202 {1203 try1204 {1205 CallPattern.CheckMethodCompatibility(method);1206 CallPattern.CheckInstrumentationAvailability(method);1207 ProfilerInterceptor.RequestReJit(method);1208 }1209 catch (MockException e)1210 {1211#if DEBUG1212 System.Diagnostics.Trace.WriteLine(string.Format("Method {0} is skipped for interception: {1}", method, e));1213#endif1214 }1215 }1216 }1217#endif1218 internal void EnableInterception(Type typeToIntercept)1219 {1220 if (ProfilerInterceptor.IsProfilerAttached)1221 {1222 for (var type = typeToIntercept; type != null;)1223 {1224 if (!ProfilerInterceptor.TypeSupportsInstrumentation(type))1225 DebugView.TraceEvent(IndentLevel.Warning, () => String.Format("Elevated mocking for type {0} will not be available due to limitations in CLR", type));1226 if (this.arrangedTypes.Add(type))1227 {1228 ProfilerInterceptor.EnableInterception(type, true, this);1229#if !PORTABLE1230 if (ProfilerInterceptor.IsReJitEnabled && type == typeToIntercept)1231 {1232 RequestRejitForTypeMethods(type);1233 }1234#endif1235 }1236 type = type.BaseType;1237 if (type == typeof(object) || type == typeof(ValueType) || type == typeof(Enum))1238 break;1239 }1240 using (this.sharedContext.StartRunClassConstructor())1241 {1242 var handle = typeToIntercept.TypeHandle;1243 this.disabledTypes.Add(typeof(RuntimeHelpers));1244 ProfilerInterceptor.RunClassConstructor(handle);1245 this.disabledTypes.Remove(typeof(RuntimeHelpers));1246 }1247 }1248 }1249 internal void DisableInterception(Type typeToIntercept)1250 {1251 if (ProfilerInterceptor.IsProfilerAttached)1252 {1253 if (this.arrangedTypes.Remove(typeToIntercept))1254 {1255 ProfilerInterceptor.EnableInterception(typeToIntercept, false, this);1256 }1257 this.disabledTypes.Add(typeToIntercept);1258 }1259 }1260 private List<MethodMockMatcherTreeNode> GetMethodMocksFromObject(object mock, Type mockType = null)1261 {1262 MockingUtil.UnwrapDelegateTarget(ref mock);1263 var methodMocks = new List<MethodMockMatcherTreeNode>();1264 var visitedMocks = new List<object>(); // can't be HashSet because we can't depend on GetHashCode being implemented properly1265 GetMethodMocksFromObjectInternal(mock, mockType, methodMocks, visitedMocks);1266 return methodMocks;1267 }1268 private void GetMethodMocksFromObjectInternal(object mock, Type mockType, List<MethodMockMatcherTreeNode> methodMocks, List<object> visitedMocks)1269 {1270 if (visitedMocks.Contains(mock))1271 return;1272 visitedMocks.Add(mock);1273 IMatcher instanceMatcher;1274 Func<MethodInfoMatcherTreeNode, bool> rootMatcher;1275 if (mockType != null)1276 {1277 instanceMatcher = new AnyMatcher();1278 rootMatcher = node => mockType.IsAssignableFrom(node.MethodInfo.DeclaringType);1279 }1280 else1281 {1282 instanceMatcher = new ValueMatcher(mock);1283 rootMatcher = node =>1284 {1285 foreach (var child in node.Children)1286 {1287 if (child.Matcher.Matches(instanceMatcher))1288 {1289 return true;1290 }1291 }1292 return false;1293 };1294 }1295 foreach (var funcRoot in arrangementTreeRoots.Values.Where(rootMatcher))1296 {1297 var callPattern = CallPattern.CreateUniversalCallPattern(funcRoot.MethodInfo);1298 callPattern.InstanceMatcher = instanceMatcher;1299 var results = funcRoot.GetAllMethodMocks(callPattern);1300 methodMocks.AddRange(results);1301 }1302 if (mock != null)1303 {1304 var mockMixin = GetMockMixin(mock, null);1305 if (mockMixin != null)1306 {1307 foreach (var dependentMock in mockMixin.DependentMocks)1308 GetMethodMocksFromObjectInternal(dependentMock, null, methodMocks, visitedMocks);1309 }1310 }1311 }1312 private List<MethodMockMatcherTreeNode> GetAllMethodMocks()1313 {1314 var methodMocks = new List<MethodMockMatcherTreeNode>();1315 GetAllMethodMocksInternal(methodMocks);1316 return methodMocks;1317 }1318 private void GetAllMethodMocksInternal(List<MethodMockMatcherTreeNode> methodMocks)1319 {1320 foreach (var funcRoot in arrangementTreeRoots.Values)1321 {1322 var callPattern = CallPattern.CreateUniversalCallPattern(funcRoot.MethodInfo);1323 var results = funcRoot.GetAllMethodMocks(callPattern);1324 methodMocks.AddRange(results);1325 }1326 }1327 private bool DispatchInvocationToMethodMocks(Invocation invocation)1328 {1329 CallPattern callPattern = CallPatternCreator.FromInvocation(invocation);1330 MethodInfoMatcherTreeNode funcRoot = null;1331 if (!invocation.InArrange && !invocation.InAssertSet)1332 {1333 if (!invocationTreeRoots.TryGetValue(callPattern.Method, out funcRoot))1334 {1335 funcRoot = new MethodInfoMatcherTreeNode(callPattern.Method);1336 invocationTreeRoots.Add(callPattern.Method, funcRoot);1337 }1338 }1339 var methodMock = DispatchInvocationToArrangements(callPattern, invocation);1340 if (!invocation.InArrange && !invocation.InAssertSet)1341 {1342 funcRoot.AddOrUpdateOccurence(callPattern, methodMock);1343 }1344 return methodMock != null;1345 }1346 private IMethodMock DispatchInvocationToArrangements(CallPattern callPattern, Invocation invocation)1347 {1348 MethodInfoMatcherTreeNode arrangeFuncRoot;1349 var methodMockNodes = new List<MethodMockMatcherTreeNode>();1350 var allMethods = new[] { callPattern.Method }1351 .Concat(callPattern.Method.GetInheritanceChain().Where(m => m.DeclaringType.IsInterface));1352 foreach (var method in allMethods)1353 {1354 DebugView.TraceEvent(IndentLevel.MethodMatch, () => String.Format("Inspect arrangements on {0} on {1}", method, method.DeclaringType));1355 if (!arrangementTreeRoots.TryGetValue(method, out arrangeFuncRoot))1356 continue;1357 var results = arrangeFuncRoot.GetMethodMock(callPattern);1358 methodMockNodes.AddRange(results);1359 }1360 var methodMock = GetMethodMockFromNodes(methodMockNodes, invocation);1361 if (methodMock == null)1362 {1363 DebugView.TraceEvent(IndentLevel.MethodMatch, () => "No arrangement chosen");1364 return null;1365 }1366 DebugView.TraceEvent(IndentLevel.MethodMatch, () => String.Format("Chosen arrangement (id={0}) {1}",1367 methodMockNodes.First(node => node.MethodMock == methodMock).Id, methodMock.ArrangementExpression));1368 methodMock.IsUsed = true; //used to correctly determine inSequence arranges1369 var behaviorsToProcess = GetBehaviorsToProcess(invocation, methodMock);1370 foreach (var behavior in behaviorsToProcess)1371 {1372 behavior.Process(invocation);1373 }1374 return methodMock;1375 }1376 private static List<IBehavior> GetBehaviorsToProcess(Invocation invocation, IMethodMock methodMock)1377 {1378 var behaviorsToExecute = new List<IBehavior>();1379 var behaviorTypesToSkip = GetBehaviorTypesToSkip(invocation);1380 behaviorsToExecute.AddRange(1381 methodMock.Behaviors.Where(behavior => !behaviorTypesToSkip.Contains(behavior.GetType())));1382 var mock = invocation.MockMixin;1383 if (mock != null)1384 {1385 behaviorsToExecute.AddRange(mock.SupplementaryBehaviors);1386#if !PORTABLE1387 // explicitly add recursive mocking behavior for ref returns in order to set invocation result1388 if (invocation.Method.GetReturnType().IsByRef)1389 {1390 behaviorsToExecute.AddRange(1391 mock.FallbackBehaviors.Where(1392 behavior =>1393 behavior is CallOriginalBehavior1394 || (behavior is RecursiveMockingBehavior && ((RecursiveMockingBehavior)behavior).Type != RecursiveMockingBehaviorType.OnlyDuringAnalysis)));1395 }1396#endif1397 }1398 return behaviorsToExecute;1399 }1400 private static List<Type> GetBehaviorTypesToSkip(Invocation invocation)1401 {1402 var behaviorTypesToSkip = new List<Type>();1403 if (invocation.InAssertSet)1404 {1405 behaviorTypesToSkip.Add(typeof(InvocationOccurrenceBehavior));1406 }1407 return behaviorTypesToSkip;1408 }1409 private bool TryCreateDelegate(Type type, MockCreationSettings settings, out object delegateResult)1410 {1411 delegateResult = null;1412 if (!typeof(Delegate).IsAssignableFrom(type) || type == typeof(Delegate) || type == typeof(MulticastDelegate))1413 return false;1414 var backendType = mockFactory.CreateDelegateBackend(type);1415 var backend = Create(backendType, settings);1416 delegateResult = Delegate.CreateDelegate(type, backend, backendType.GetMethod("Invoke"));1417 return true;1418 }1419 private IMethodMock GetMethodMockFromNodes(List<MethodMockMatcherTreeNode> methodMockNodes, Invocation invocation)1420 {1421 if (methodMockNodes.Count == 0)1422 {1423 return null;1424 }1425 var resultList =1426 methodMockNodes1427 .OrderBy(x => x.Id)1428 .Select(1429 x =>1430 new1431 {1432 x.MethodMock,1433 Acceptable = new Lazy<bool>(() => x.MethodMock.AcceptCondition == null1434 || (bool)x.MethodMock.AcceptCondition.CallOverride(invocation))1435 })1436 .ToList();1437 var isInOrderOverwrites =1438 resultList.Count() > 01439 && resultList.Where(x => x.MethodMock.IsInOrder).Any()1440 && resultList.Last().MethodMock.IsInOrder;1441 // if one or more InOrder arrangement overwrites other ones, then skip selecting others1442 if (!isInOrderOverwrites)1443 {1444 var nonSequentialOrInOrder =1445 resultList.Where(x => !x.MethodMock.IsSequential && !x.MethodMock.IsInOrder && x.Acceptable).LastOrDefault();1446 if (nonSequentialOrInOrder != null)1447 {1448 return nonSequentialOrInOrder.MethodMock;1449 }1450 }1451 var inOrder = resultList.Where(x => x.MethodMock.IsInOrder && !x.MethodMock.IsUsed && x.Acceptable).FirstOrDefault();1452 if (inOrder != null)1453 {1454 return inOrder.MethodMock;1455 }1456 var sequential = resultList.Where(x => x.MethodMock.IsSequential && !x.MethodMock.IsUsed && x.Acceptable).FirstOrDefault();1457 if (sequential != null)1458 {1459 return sequential.MethodMock;1460 }1461 return resultList.Where(x => (x.MethodMock.IsSequential || x.MethodMock.IsInOrder) && x.Acceptable).Select(x => x.MethodMock).LastOrDefault();1462 }1463 internal string GetDebugView(object mock = null)1464 {1465 IEnumerable<MethodMockMatcherTreeNode> nodes;1466 if (mock != null)1467 {1468 nodes = GetMethodMocksFromObject(mock);1469 }1470 else1471 {1472 nodes = from kvp in this.arrangementTreeRoots1473 let universalCP = CallPattern.CreateUniversalCallPattern(kvp.Key)1474 from node in kvp.Value.GetAllMethodMocks(universalCP)1475 select node;1476 }1477 var sb = new StringBuilder();1478 sb.AppendFormat("Elevated mocking: {0}\n", ProfilerInterceptor.IsProfilerAttached ? "enabled" : "disabled");1479 sb.AppendLine("\nArrangements and expectations:");1480 bool addedStuff = false;1481 foreach (var node in nodes)1482 {1483 addedStuff = true;1484 var methodMock = node.MethodMock;1485 sb.AppendFormat(" Arrangement (id={1}) {0}:\n", methodMock.ArrangementExpression, node.Id);1486 foreach (var behavior in methodMock.Behaviors.OfType<IAssertableBehavior>())1487 {1488 var debugView = behavior.DebugView;1489 if (debugView != null)1490 sb.AppendFormat(" {0}\n", debugView);1491 }1492 }1493 if (!addedStuff)1494 sb.AppendLine(" --none--");1495 sb.AppendLine("\nInvocations:");1496 var invocations =1497 (from kvp in this.invocationTreeRoots1498 let universalCP = CallPattern.CreateUniversalCallPattern(kvp.Key)1499 from node in kvp.Value.GetOccurences(universalCP)1500 select node).ToList();...

Full Screen

Full Screen

InvocationOccurrenceBehavior.cs

Source:InvocationOccurrenceBehavior.cs Github

copy

Full Screen

...21using Debug = System.Diagnostics.Debug;22#endif23namespace Telerik.JustMock.Core.Behaviors24{25 internal class InvocationOccurrenceBehavior : IAssertableBehavior26 {27 private readonly IMethodMock methodMock;28 public int? LowerBound { get; set; }29 public int? UpperBound { get; set; }30 private string message;31 private int calls;32 public string DebugView33 {34 get35 {36 if ((LowerBound == null || LowerBound <= 0) && (UpperBound == null))37 return null;38 return String.Format("{3}: Occurences must be in [{0}, {1}]; calls so far: {2}. {4}",39 LowerBound.HasValue ? (object)LowerBound.Value : "any",40 UpperBound.HasValue ? (object)UpperBound.Value : "any",41 calls,42 IsInRange(LowerBound, UpperBound, calls) ? "Met" : "Unmet",43 this.message ?? "");44 }45 }46 public InvocationOccurrenceBehavior(IMethodMock methodMock)47 {48 this.methodMock = methodMock;49 }50 public void SetBounds(int? lowerBound, int? upperBound, string message)51 {52 this.LowerBound = lowerBound;53 this.UpperBound = upperBound;54 this.message = message;55 }56 public void Process(Invocation invocation)57 {58 ++calls;59 Telerik.JustMock.DebugView.TraceEvent(IndentLevel.DispatchResult, () => String.Format("Calls so far: {0}", calls));60 Assert(null, this.UpperBound, calls, this.message, null);61 }62 public void Assert()63 {64 Assert(this.LowerBound, this.UpperBound);65 }66 public void Assert(int? lowerBound, int? upperBound)67 {68 var expr = this.methodMock.ArrangementExpression;69 Assert(lowerBound, upperBound, this.calls, this.message, expr);70 }71 public static void Assert(int? lowerBound, int? upperBound, int calls, string userMessage, object expression)72 {73 if (IsInRange(lowerBound, upperBound, calls))74 return;75 var message = String.Format("{2}Occurrence expectation failed. {0}. Calls so far: {1}",76 MakeRangeString(lowerBound, upperBound),77 calls,78 userMessage != null ? userMessage + " " : string.Empty);79 if (expression != null)80 message += String.Format("\nArrange expression: {0}", expression).EscapeFormatString();81 MockingContext.Fail(message);82 }83 private static bool IsInRange(int? lowerBound, int? upperBound, int calls)84 {85 bool withinLowerBound = !lowerBound.HasValue || calls >= lowerBound.Value;86 bool withinUpperBound = !upperBound.HasValue || calls <= upperBound.Value;87 return withinLowerBound && withinUpperBound;88 }89 private static string MakeRangeString(int? lowerBound, int? upperBound)90 {91 Debug.Assert(lowerBound != null || upperBound != null);92 return upperBound == 0 ? "Expected no calls"93 : lowerBound != null && lowerBound == upperBound ? String.Format("Expected exactly {0} call{1}", lowerBound, lowerBound != 1 ? "s" : "")94 : lowerBound != null && upperBound != null ? String.Format("Expected between {0} and {1} calls", lowerBound, upperBound)95 : lowerBound != null ? String.Format("Expected at least {0} call{1}", lowerBound, lowerBound != 1 ? "s" : "")96 : upperBound != null ? String.Format("Expected at most {0} call{1}", upperBound, upperBound != 1 ? "s" : "")97 : null;98 }99 }100}...

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock;7using Telerik.JustMock.Helpers;8using Telerik.JustMock.Core.Behaviors;9{10 {11 int GetEmployeeId();12 string GetEmployeeName();13 }14 {15 public int EmployeeId { get; set; }16 public string EmployeeName { get; set; }17 public int GetEmployeeId()18 {19 return EmployeeId;20 }21 public string GetEmployeeName()22 {23 return EmployeeName;24 }25 }26 {27 static void Main(string[] args)28 {29 var employee = Mock.Create<Employee>();30 Mock.Arrange(() => employee.GetEmployeeId()).Returns(1).Assert();31 Mock.Arrange(() => employee.GetEmployeeName()).Returns("Name").Assert();32 Console.WriteLine(employee.GetEmployeeId());33 Console.WriteLine(employee.GetEmployeeName());34 Mock.Assert(employee);35 Console.ReadLine();36 }37 }38}

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock;7using Telerik.JustMock.Core;8using Telerik.JustMock.Helpers;9{10 {11 static void Main(string[] args)12 {13 var mock = Mock.Create<IFoo>();14 Mock.Arrange(() => mock.Execute(Arg.AnyString)).Returns("Foo");15 var result = mock.Execute("Foo");16 Mock.Assert(() => mock.Execute(Arg.AnyString), Occurs.Exactly(1));17 }18 }19 {20 string Execute(string input);21 }22}23using System;24using System.Collections.Generic;25using System.Linq;26using System.Text;27using System.Threading.Tasks;28using Telerik.JustMock;29using Telerik.JustMock.Core;30using Telerik.JustMock.Helpers;31{32 {33 static void Main(string[] args)34 {35 var mock = Mock.Create<IFoo>();36 Mock.Arrange(() => mock.Execute(Arg.AnyString)).Returns("Foo");37 var result = mock.Execute("Foo");38 Mock.Assert(() => mock.Execute(Arg.AnyString), Occurs.Never());39 }40 }41 {42 string Execute(string input);43 }44}45using System;46using System.Collections.Generic;47using System.Linq;48using System.Text;49using System.Threading.Tasks;50using Telerik.JustMock;51using Telerik.JustMock.Core;52using Telerik.JustMock.Helpers;53{54 {55 static void Main(string[] args)56 {57 var mock = Mock.Create<IFoo>();58 Mock.Arrange(() => mock.Execute(Arg.AnyString)).Returns("Foo");59 var result = mock.Execute("Foo");60 Mock.Assert(() => mock.Execute(Arg.AnyString), Occurs.AtLeastOnce());61 }62 }63 {64 string Execute(string input);65 }66}

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Forms;7using Telerik.JustMock;8using Telerik.JustMock.Core;9using Telerik.JustMock.Helpers;10{11 {12 public void TestMethod()13 {14 var mock = Mock.Create<TestClass>();15 Mock.Arrange(() => mock.TestMethod()).Assert();16 }17 }18}19using System;20using System.Collections.Generic;21using System.Linq;22using System.Text;23using System.Threading.Tasks;24using System.Windows.Forms;25using Telerik.JustMock;26using Telerik.JustMock.Core;27using Telerik.JustMock.Helpers;28{29 {30 public void TestMethod()31 {32 var mock = Mock.Create<TestClass>();33 Mock.Arrange(() => mock.TestMethod()).Assert(() => mock.TestMethod() == 1);34 }35 }36}37using System;38using System.Collections.Generic;39using System.Linq;40using System.Text;41using System.Threading.Tasks;42using System.Windows.Forms;43using Telerik.JustMock;44using Telerik.JustMock.Core;45using Telerik.JustMock.Helpers;46{47 {48 public void TestMethod()49 {50 var mock = Mock.Create<TestClass>();51 Mock.Arrange(() => mock.TestMethod()).Assert(() => mock.TestMethod() == 1, "Message");52 }53 }54}55using System;56using System.Collections.Generic;57using System.Linq;58using System.Text;59using System.Threading.Tasks;60using System.Windows.Forms;61using Telerik.JustMock;62using Telerik.JustMock.Core;63using Telerik.JustMock.Helpers;64{65 {66 public void TestMethod()67 {68 var mock = Mock.Create<TestClass>();69 Mock.Arrange(() => mock.TestMethod()).Assert(() => mock.TestMethod() == 1, "Message

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock;7using Telerik.JustMock.Core;8using NUnit.Framework;9{10 {11 public int Add(int a, int b)12 {13 return a + b;14 }15 }16 {17 public void TestMethod()18 {19 var obj = Mock.Create<Class1>();20 Mock.Arrange(() => obj.Add(Arg.AnyInt, Arg.AnyInt)).Returns(10);21 Assert.AreEqual(10, obj.Add(1, 2));22 Mock.Assert(obj).WasCalled(x => x.Add(Arg.AnyInt, Arg.AnyInt));23 }24 }25}26using System;27using System.Collections.Generic;28using System.Linq;29using System.Text;30using System.Threading.Tasks;31using Telerik.JustMock;32using Telerik.JustMock.Core;33using NUnit.Framework;34{35 {36 public int Add(int a, int b)37 {38 return a + b;39 }40 }41 {42 public void TestMethod()43 {44 var obj = Mock.Create<Class1>();45 Mock.Arrange(() => obj.Add(Arg.AnyInt, Arg.AnyInt)).Returns(10);46 Assert.AreEqual(10, obj.Add(1, 2));47 Mock.Assert(obj).WasCalled(x => x.Add(Arg.AnyInt, Arg.AnyInt)).Exactly(1);48 }49 }50}51using System;52using System.Collections.Generic;53using System.Linq;54using System.Text;55using System.Threading.Tasks;56using Telerik.JustMock;57using Telerik.JustMock.Core;58using NUnit.Framework;59{60 {61 public int Add(int a, int b)62 {63 return a + b;64 }65 }66 {67 public void TestMethod()68 {

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Behaviors;2using System;3using System.Text;4using System.Collections.Generic;5using System.Linq;6using Microsoft.VisualStudio.TestTools.UnitTesting;7{8{9public void TestMethod1()10{11var mock = Mock.Create<TestClass>();12Mock.Arrange(() => mock.TestMethod()).MustBeCalled();13mock.TestMethod();14Assert.AreEqual(1, mock.TestMethod().GetInvocationCount());15}16}17}18using Telerik.JustMock.Core.Behaviors;19using System;20using System.Text;21using System.Collections.Generic;22using System.Linq;23using Microsoft.VisualStudio.TestTools.UnitTesting;24{25{26public void TestMethod1()27{28var mock = Mock.Create<TestClass>();29Mock.Arrange(() => mock.TestMethod()).MustBeCalled();30mock.TestMethod();31Assert.AreEqual(1, mock.TestMethod().GetInvocationCount());32}33}34}35using Telerik.JustMock.Core.Behaviors;36using System;37using System.Text;38using System.Collections.Generic;39using System.Linq;40using Microsoft.VisualStudio.TestTools.UnitTesting;41{42{43public void TestMethod1()44{45var mock = Mock.Create<TestClass>();46Mock.Arrange(() => mock.TestMethod()).MustBeCalled();47mock.TestMethod();48Assert.AreEqual(1, mock.TestMethod().GetInvocationCount());49}50}51}52using Telerik.JustMock.Core.Behaviors;53using System;54using System.Text;55using System.Collections.Generic;56using System.Linq;57using Microsoft.VisualStudio.TestTools.UnitTesting;58{59{60public void TestMethod1()61{62var mock = Mock.Create<TestClass>();63Mock.Arrange(() => mock.TestMethod()).MustBeCalled();64mock.TestMethod();65Assert.AreEqual(1, mock.TestMethod().GetInvocationCount());66}67}68}

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1public void TestMethod1()2{3 var mock = Mock.Create<IFoo>();4 Mock.Arrange(() => mock.Execute()).MustBeCalled();5 mock.Execute();6 Mock.Assert(mock);7}8public void TestMethod1()9{10 var mock = Mock.Create<IFoo>();11 Mock.Arrange(() => mock.Execute()).MustBeCalled();12 mock.Execute();13 Mock.Assert(mock);14}15public void TestMethod1()16{17 var mock = Mock.Create<IFoo>();18 Mock.Arrange(() => mock.Execute()).MustBeCalled();19 mock.Execute();20 Mock.Assert(mock);21}22public void TestMethod1()23{24 var mock = Mock.Create<IFoo>();25 Mock.Arrange(() => mock.Execute()).MustBeCalled();26 mock.Execute();27 Mock.Assert(mock);28}29public void TestMethod1()30{31 var mock = Mock.Create<IFoo>();32 Mock.Arrange(() => mock.Execute()).MustBeCalled();33 mock.Execute();34 Mock.Assert(mock);35}36public void TestMethod1()37{38 var mock = Mock.Create<IFoo>();39 Mock.Arrange(() => mock.Execute()).MustBeCalled();40 mock.Execute();41 Mock.Assert(mock);42}43public void TestMethod1()44{45 var mock = Mock.Create<IFoo>();46 Mock.Arrange(() => mock.Execute()).MustBeCalled();47 mock.Execute();48 Mock.Assert(mock);49}

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using System;2using Telerik.JustMock;3using Telerik.JustMock.Core;4using Telerik.JustMock.Helpers;5{6{7public static void Main()8{9var mock = Mock.Create<IFoo>();10Mock.Arrange(() => mock.Execute(Arg.AnyString)).MustBeCalled();11Mock.Assert(mock);12Console.WriteLine("Hello World!");13}14}15}16Error 1 The type or namespace name 'Behaviors' does not exist in the namespace 'Telerik.JustMock.Core' (are you missing an assembly reference?) C:\Users\user\Documents\Visual Studio 2012\Projects\JustMockUnitTestProject1\JustMockUnitTestProject1\1.cs 8 28 JustMockUnitTestProject1

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Behaviors;2public void Method1()3{4 var mock = Mock.Create<ISomeInterface>();5 Mock.Arrange(() => mock.Method1()).Returns(1);6 Mock.Arrange(() => mock.Method2()).Assert();7 var result = mock.Method1();8 mock.Method2();9}10using Telerik.JustMock.Core.Behaviors;11public void Method1()12{13 var mock = Mock.Create<ISomeInterface>();14 Mock.Arrange(() => mock.Method1()).Returns(1);15 Mock.Arrange(() => mock.Method2()).Assert();16 var result = mock.Method1();17 mock.Method2();18}19using Telerik.JustMock.Core.Behaviors;20public void Method1()21{22 var mock = Mock.Create<ISomeInterface>();23 Mock.Arrange(() => mock.Method1()).Returns(1);24 Mock.Arrange(() => mock.Method2()).Assert();25 var result = mock.Method1();26 mock.Method2();27}28using Telerik.JustMock.Core.Behaviors;29public void Method1()30{31 var mock = Mock.Create<ISomeInterface>();32 Mock.Arrange(() => mock.Method1()).Returns(1);33 Mock.Arrange(() => mock.Method2()).Assert();34 var result = mock.Method1();35 mock.Method2();36}37using Telerik.JustMock.Core.Behaviors;38public void Method1()39{40 var mock = Mock.Create<ISomeInterface>();41 Mock.Arrange(() => mock.Method1()).Returns(1);42 Mock.Arrange(() => mock.Method2()).Assert();43 var result = mock.Method1();44 mock.Method2();45}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in InvocationOccurrenceBehavior

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful