|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using Microsoft.EntityFrameworkCore.ChangeTracking; |
| 3 | +using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; |
| 4 | +using Microsoft.EntityFrameworkCore.Metadata; |
| 5 | +using Moq; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Data.Entity; |
| 9 | +using System.Linq; |
| 10 | +using System.Text; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using WebGoatCore.Data; |
| 13 | +using WebGoatCore.Models; |
| 14 | + |
| 15 | +namespace WebGoat.NET.Tests.BlogRespRepositoryTests |
| 16 | +{ |
| 17 | + internal static class ContextSetup |
| 18 | + { |
| 19 | + internal static Mock<NorthwindContext> CreateContext() |
| 20 | + { |
| 21 | + // create test DB |
| 22 | + var context = BlogRepositoryTests.ContextSetup.CreateContext(); |
| 23 | + |
| 24 | + var blogEntryRepo = new BlogEntryRepository(context.Object); |
| 25 | + |
| 26 | + var entry1 = blogEntryRepo.GetBlogEntry(1); |
| 27 | + |
| 28 | + var r1 = new BlogResponse() { Author = "admin", Contents = "Test Content", Id = 1, ResponseDate = DateTime.Now, BlogEntry = entry1, BlogEntryId = entry1.Id }; |
| 29 | + var r2 = new BlogResponse() { Author = "kmitnick", Contents = "KM Test Content", Id = 2, ResponseDate = DateTime.Now, BlogEntry = entry1, BlogEntryId = entry1.Id }; |
| 30 | + var r3 = new BlogResponse() { Author = "me", Contents = "ME Test Content", Id = 3, ResponseDate = DateTime.Now, BlogEntry = entry1, BlogEntryId = entry1.Id }; |
| 31 | + |
| 32 | + entry1.Responses.Add(r1); |
| 33 | + entry1.Responses.Add(r2); |
| 34 | + entry1.Responses.Add(r3); |
| 35 | + |
| 36 | + var entriesList = new List<BlogResponse> { |
| 37 | + r1, r2, r3 |
| 38 | + }; |
| 39 | + var initialBlogEntries = entriesList.AsQueryable(); |
| 40 | + |
| 41 | + Func<BlogResponse, EntityEntry<BlogResponse>> mockEntityEntry = (BlogResponse data) => |
| 42 | + { |
| 43 | + var internalEntityEntry = new InternalEntityEntry( |
| 44 | + new Mock<IStateManager>().Object, |
| 45 | + new RuntimeEntityType(nameof(BlogResponse), typeof(BlogResponse), false, null, null, null, ChangeTrackingStrategy.Snapshot, null, false), |
| 46 | + data); |
| 47 | + |
| 48 | + var entityEntry = new EntityEntry<BlogResponse>(internalEntityEntry); |
| 49 | + return entityEntry; |
| 50 | + }; |
| 51 | + |
| 52 | + var mockSet = DbSetTestUtil.CreateDbSetMock(initialBlogEntries); |
| 53 | + |
| 54 | + mockSet.Setup(m => m.Add(It.IsAny<BlogResponse>())).Returns((BlogResponse b) => |
| 55 | + { |
| 56 | + entriesList.Add(b); |
| 57 | + return mockEntityEntry(b); |
| 58 | + }); |
| 59 | + |
| 60 | + context.SetupGet(c => c.BlogResponses).Returns(mockSet.Object); |
| 61 | + |
| 62 | + return context; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + } |
| 67 | +} |
0 commit comments