Exemple #1
0
        /// <summary>
        /// Begins expecting callbacks. When the returned scope is disposed, stops expecting callbacks and throws <see cref="AssertionException"/>
        /// if <see cref="OnCallback"/> was not called the expected number of times between beginning and ending.
        /// </summary>
        /// <param name="count">
        /// The number of times that <see cref="OnCallback"/> is expected to be called before the returned scope is disposed.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="count"/> is less than 1.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the returned scope from the previous call has not been disposed.</exception>
        /// <exception cref="AssertionException">Thrown when <see cref="OnCallback"/> was not called the expected number of times between beginning and ending.</exception>
        public IDisposable ExpectCallback(int count = 1)
        {
            if (count < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(count), expectedCount, "Expected callback count must be greater than or equal to one.");
            }

            if (expectedCount != 0)
            {
                throw new InvalidOperationException($"The previous {nameof(ExpectCallback)} scope must be disposed before calling again.");
            }

            expectedCount = count;
            actualCount   = 0;

            return(On.Dispose(() =>
            {
                try
                {
                    if (actualCount < expectedCount)
                    {
                        Assert.Fail($"Expected {(expectedCount == 1 ? "a single call" : expectedCount + " calls")}, but there {(actualCount == 1 ? "was" : "were")} {actualCount}.");
                    }
                }
                finally
                {
                    expectedCount = 0;
                }
            }));
        }
Exemple #2
0
        public static IDisposable WithTemporarySynchronizationContext(SynchronizationContext?context)
        {
            var originalSynchronizationContext = SynchronizationContext.Current;

            SynchronizationContext.SetSynchronizationContext(context);

            return(On.Dispose(() =>
            {
                if (SynchronizationContext.Current == context)
                {
                    SynchronizationContext.SetSynchronizationContext(originalSynchronizationContext);
                }
            }));
        }
Exemple #3
0
        public static IDisposable ExpectSinglePost(Action <Action> testPostedAction)
        {
            if (testPostedAction is null)
            {
                throw new ArgumentNullException(nameof(testPostedAction));
            }

            var context         = new MockSynchronizationContext(testPostedAction);
            var withTempContext = Utils.WithTemporarySynchronizationContext(context);

            return(On.Dispose(() =>
            {
                withTempContext.Dispose();
                if (!context.ReceivedPost)
                {
                    Assert.Fail("Expected a call to SynchronizationContext.Post.");
                }
            }));
        }