示例#1
0
        public async Task It_throws_object_disposed_exception(bool async)
        {
            var context = new DbContext(new DbContextOptions <DbContext>());

            if (async)
            {
                await context.DisposeAsync();
            }
            else
            {
                context.Dispose();
            }

            // methods (tests all paths)
            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.Add(new object())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.Find(typeof(Random), 77)).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.Attach(new object())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.Update(new object())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.Remove(new object())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.SaveChanges()).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                (await Assert.ThrowsAsync <ObjectDisposedException>(() => context.SaveChangesAsync())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                (await Assert.ThrowsAsync <ObjectDisposedException>(() => context.AddAsync(new object()).AsTask())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                (await Assert.ThrowsAsync <ObjectDisposedException>(() => context.FindAsync(typeof(Random), 77).AsTask())).Message);

            var methodCount         = typeof(DbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Count();
            var expectedMethodCount = 42 + 8;

            Assert.True(
                methodCount == expectedMethodCount,
                userMessage: $"Expected {expectedMethodCount} methods on DbContext but found {methodCount}. "
                + "Update test to ensure all methods throw ObjectDisposedException after dispose.");

            // getters
            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.ChangeTracker).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.Model).Message);

            var expectedProperties = new List <string>
            {
                nameof(DbContext.ChangeTracker),
                nameof(DbContext.ContextId), // By-design, does not throw for disposed context
                nameof(DbContext.Database),
                nameof(DbContext.Model)
            };

            Assert.True(
                expectedProperties.SequenceEqual(
                    typeof(DbContext)
                    .GetProperties()
                    .Select(p => p.Name)
                    .OrderBy(s => s)
                    .ToList()),
                userMessage: "Unexpected properties on DbContext. "
                + "Update test to ensure all getters throw ObjectDisposedException after dispose.");

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => ((IInfrastructure <IServiceProvider>)context).Instance).Message);
        }
 public void Dispose()
 {
     _dbContext?.Dispose();
     GC.SuppressFinalize(this);
 }