コード例 #1
0
        public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
        {
            var stackTrace = new StackTrace();

            MethodBase callingMethodOutsideJustmock = null;

            foreach (var method in stackTrace.EnumerateFrames())
            {
                if (contextMethod == method)
                {
                    return(contextRepository);
                }

                if (callingMethodOutsideJustmock == null && method.Module.Assembly != typeof(MocksRepository).Assembly)
                {
                    callingMethodOutsideJustmock = method;
                }
            }

            if (callingMethodOutsideJustmock != null && unresolvedContextBehavior == UnresolvedContextBehavior.CreateNewContextualOrLocal)
            {
                // don't reset the old repository - because the mocks created from it may still be used, e.g. if the method
                // associated with ResolveRepository works as a factory for mocks which are then exercised elsewhere
                contextMethod     = callingMethodOutsideJustmock;
                contextRepository = new MocksRepository(null, contextMethod);

                return(contextRepository);
            }

            return(null);
        }
コード例 #2
0
        public override MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
        {
            lock (this.repositorySync)
            {
                var testMethod = this.GetTestMethod();
                if (testMethod != null)
                {
                    return(repositories[testMethod.DeclaringType]);
                }

                if (unresolvedContextBehavior == UnresolvedContextBehavior.DoNotCreateNew)
                {
                    return(null);
                }

                var stackTrace     = new StackTrace();
                var frames         = stackTrace.EnumerateFrames().ToList();
                var caller         = frames.FirstOrDefault(method => method.Module.Assembly != typeof(MocksRepository).Assembly);
                var mspecTestClass = caller.DeclaringType;

                MocksRepository parentRepo;
                repositories.TryGetValue(mspecTestClass.BaseType, out parentRepo);

                var repo = new MocksRepository(parentRepo, caller);
                repositories.Add(mspecTestClass, repo);

                return(repo);
            }
        }
コード例 #3
0
		public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
		{
			var stackTrace = new StackTrace();

			MethodBase callingMethodOutsideJustmock = null;
			foreach (var method in stackTrace.EnumerateFrames())
			{
				if (contextMethod == method)
					return contextRepository;

				if (callingMethodOutsideJustmock == null && method.Module.Assembly != typeof(MocksRepository).Assembly)
					callingMethodOutsideJustmock = method;
			}

			if (callingMethodOutsideJustmock != null && unresolvedContextBehavior == UnresolvedContextBehavior.CreateNewContextualOrLocal)
			{
				// don't reset the old repository - because the mocks created from it may still be used, e.g. if the method
				// associated with ResolveRepository works as a factory for mocks which are then exercised elsewhere
				contextMethod = callingMethodOutsideJustmock;
				contextRepository = new MocksRepository(null, contextMethod);

				return contextRepository;
			}

			return null;
		}
コード例 #4
0
		public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
		{
			if (repository == null)
			{
				repository = new MocksRepository(null, null);
			}
			return repository;
		}
コード例 #5
0
 public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
 {
     if (repository == null)
     {
         repository = new MocksRepository(null, null);
     }
     return(repository);
 }
		public override MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
		{
			RepositoryOperationsBase entryOps = null;
			int repoIdx;
			var testMethod = FindTestMethod(out repoIdx, out entryOps);
			if (testMethod == null)
				return null;

			var entryKey = entryOps.GetKey(testMethod);

			MocksRepository repo = FindRepositoryInOps(entryOps, entryKey);
			if (repo != null)
				return repo;
			if (unresolvedContextBehavior == UnresolvedContextBehavior.DoNotCreateNew)
				return null;

			//Check if this is the same kind of method but from a derived class, thus building context.
			MocksRepository parentRepo = entryOps.FindRepositoryToInherit(testMethod);
			if (parentRepo == null)
			{
				for (var repoIdxParent = repoIdx + 1; parentRepo == null && repoIdxParent < this.repoOperations.Count; ++repoIdxParent)
				{
					var ops = this.repoOperations[repoIdxParent];
					if (ops.IsLeaf)
						continue;

					var parentKey = ops.GetKey(testMethod);
					if (ops.IsUsedOnAllThreads)
						parentRepo = ops.FindRepositoryFromAnyThread(parentKey);
					else
						parentRepo = ops.FindRepository(parentKey) ?? ops.FindRepositoryToInherit(testMethod);
				}
			}

			MocksRepository entryRepo;
			try
			{
				entryRepo = new MocksRepository(parentRepo, testMethod);
				entryOps.AddRepository(entryKey, entryRepo);
				OnMocksRepositoryCreated(repo);
			}
			catch (TypeInitializationException e)
			{
				throw e.InnerException;
			}

			return entryRepo;
		}
コード例 #7
0
		public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
		{
			foreach (var resolver in registeredContextResolvers)
			{
				var repo = resolver.ResolveRepository(unresolvedContextBehavior);
				if (repo != null)
				{
					lastFrameworkAwareRepository = repo;
					return repo;
				}
			}

			if (lastFrameworkAwareRepository != null && !ProfilerInterceptor.IsProfilerAttached)
				return lastFrameworkAwareRepository;

			return LocalMockingContextResolver.ResolveRepository(unresolvedContextBehavior);
		}
コード例 #8
0
        public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
        {
            foreach (var resolver in registeredContextResolvers)
            {
                var repo = resolver.ResolveRepository(unresolvedContextBehavior);
                if (repo != null)
                {
                    lastFrameworkAwareRepository = repo;
                    return(repo);
                }
            }

            if (lastFrameworkAwareRepository != null && !ProfilerInterceptor.IsProfilerAttached)
            {
                return(lastFrameworkAwareRepository);
            }

            return(LocalMockingContextResolver.ResolveRepository(unresolvedContextBehavior));
        }
コード例 #9
0
		public override MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
		{
			var stackTrace = new StackTrace();
			var frames = stackTrace.EnumerateFrames().ToList();
			var testMethod = FindExistingTestMethod(frames);
			if (testMethod != null)
				return repositories[testMethod.DeclaringType];
			if (unresolvedContextBehavior == UnresolvedContextBehavior.DoNotCreateNew)
				return null;

			var caller = frames.FirstOrDefault(method => method.Module.Assembly != typeof(MocksRepository).Assembly);
			var mspecTestClass = caller.DeclaringType;

			MocksRepository parentRepo;
			repositories.TryGetValue(mspecTestClass.BaseType, out parentRepo);

			var repo = new MocksRepository(parentRepo, caller);
			repositories.Add(mspecTestClass, repo);

			return repo;
		}
コード例 #10
0
		public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
		{
			if (unresolvedContextBehavior != UnresolvedContextBehavior.DoNotCreateNew)
			{
				DebugView.TraceEvent(IndentLevel.StackTrace, () => String.Format("Resolving repository with unresolved context behavior {0}", unresolvedContextBehavior));
			}

			foreach (var resolver in registeredContextResolvers)
			{
				var repo = resolver.ResolveRepository(unresolvedContextBehavior);
				if (repo != null)
				{
					lastFrameworkAwareRepository = repo;
					return repo;
				}
			}

			if (lastFrameworkAwareRepository != null && !ProfilerInterceptor.IsProfilerAttached)
				return lastFrameworkAwareRepository;

			return LocalMockingContextResolver.ResolveRepository(unresolvedContextBehavior);
		}
コード例 #11
0
        public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
        {
            if (unresolvedContextBehavior != UnresolvedContextBehavior.DoNotCreateNew)
            {
                DebugView.TraceEvent(IndentLevel.StackTrace, () => String.Format("Resolving repository with unresolved context behavior {0}", unresolvedContextBehavior));
            }

            foreach (var resolver in registeredContextResolvers)
            {
                var repo = resolver.ResolveRepository(unresolvedContextBehavior);
                if (repo != null)
                {
                    lastFrameworkAwareRepository = repo;
                    return(repo);
                }
            }

            if (lastFrameworkAwareRepository != null && !ProfilerInterceptor.IsProfilerAttached)
            {
                return(lastFrameworkAwareRepository);
            }

            return(LocalMockingContextResolver.ResolveRepository(unresolvedContextBehavior));
        }
        public override MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
        {
            RepositoryOperationsBase entryOps = null;
            int repoIdx;
            var testMethod = FindTestMethod(out repoIdx, out entryOps);

            if (testMethod == null)
            {
                return(null);
            }

            var entryKey = entryOps.GetKey(testMethod);

            MocksRepository repo = FindRepositoryInOps(entryOps, entryKey);

            if (repo != null)
            {
                return(repo);
            }
            if (unresolvedContextBehavior == UnresolvedContextBehavior.DoNotCreateNew)
            {
                return(null);
            }

            //Check if this is the same kind of method but from a derived class, thus building context.
            MocksRepository parentRepo = entryOps.FindRepositoryToInherit(testMethod);

            if (parentRepo == null)
            {
                for (var repoIdxParent = repoIdx + 1; parentRepo == null && repoIdxParent < this.repoOperations.Count; ++repoIdxParent)
                {
                    var ops = this.repoOperations[repoIdxParent];
                    if (ops.IsLeaf)
                    {
                        continue;
                    }

                    var parentKey = ops.GetKey(testMethod);
                    if (ops.IsUsedOnAllThreads)
                    {
                        parentRepo = ops.FindRepositoryFromAnyThread(parentKey);
                    }
                    else
                    {
                        parentRepo = ops.FindRepository(parentKey) ?? ops.FindRepositoryToInherit(testMethod);
                    }
                }
            }

            MocksRepository entryRepo;

            try
            {
                entryRepo = new MocksRepository(parentRepo, testMethod);
                entryOps.AddRepository(entryKey, entryRepo);
                OnMocksRepositoryCreated(repo);
            }
            catch (TypeInitializationException e)
            {
                throw e.InnerException;
            }

            return(entryRepo);
        }
コード例 #13
0
 public abstract MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior);
コード例 #14
0
		public abstract MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior);