Пример #1
0
        private static Mock <IAppDbContextFactory> SetupContextFactory(IStudentsQuery studentsQuery)
        {
            var options = new DbContextOptionsBuilder <AppDbContext>()
                          .Options;
            var dbContext = new AppDbContext(options);

            QueryExtensions.StudentsQueryFactory = context => studentsQuery;

            var mockDbContextFactory = new Mock <IAppDbContextFactory>();

            mockDbContextFactory.Setup(cf => cf.Create()).Returns(dbContext);
            return(mockDbContextFactory);
        }
Пример #2
0
        private static Mock <IAppDbContextFactory> SetupContextFactory(IMessagesQuery messagesQuery, IMessageFileQuery messageFileQuery, IStudentsQuery studentsQuery)
        {
            var options = new DbContextOptionsBuilder <AppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          // don't raise the error warning us that the in memory db doesn't support transactions
                          .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning))
                          .Options;
            var dbContext = new AppDbContext(options);

            dbContext.FileTypes.AddRange(GetTestFileTypes());
            dbContext.MessageFiles.AddRange(GetTestMessageFiles());
            dbContext.Messages.AddRange(GetTestMessages());

            dbContext.SaveChanges();

            QueryExtensions.MessagesQueryFactory    = context => messagesQuery;
            QueryExtensions.MessageFileQueryFactory = context => messageFileQuery;
            QueryExtensions.StudentsQueryFactory    = context => studentsQuery;

            var mockDbContextFactory = new Mock <IAppDbContextFactory>();

            mockDbContextFactory.Setup(cf => cf.Create()).Returns(dbContext);
            return(mockDbContextFactory);
        }