public void OnModelCreating_VerifyModelCreation() { var mockModel = new Mock <ModelBuilder>(new ConventionSet()); try { var contextOptions = new DbContextOptions <TaskHandlerDbContext>(); var taskModel = new TaskItem(); var taskDbContextStub = new TaskDbContextStub(contextOptions); var modelBuilder = new ModelBuilder(new ConventionSet()); var model = new Model(); var configSource = new ConfigurationSource(); var entity = new EntityType("TaskModel", model, configSource); var internalModelBuilder = new InternalModelBuilder(model); var internalEntityTypeBuilder = new InternalEntityTypeBuilder(entity, internalModelBuilder); var entityTypeBuilder = new EntityTypeBuilder <TaskItem>(internalEntityTypeBuilder); mockModel.Setup(m => m.Entity <TaskItem>()).Returns(entityTypeBuilder); var property = new Property("Name", taskModel.GetType(), taskModel.GetType().GetProperty("Name"), taskModel.GetType().GetField("Name"), entity, configSource, null); var internalPropertyBuilder = new InternalPropertyBuilder(property, internalModelBuilder); var propertyBuilder = new PropertyBuilder <string>(internalPropertyBuilder); taskDbContextStub.TestModelCreation(modelBuilder); } catch (Exception ex) { mockModel.Verify(m => m.Entity <TaskItem>().HasKey("Id"), Times.Once); Assert.NotNull(ex); } }
private List <TaskChanges> GetDiff(TaskItem oldTask, TaskItem newTask, string newCategories) { List <TaskChanges> changes = new List <TaskChanges>(); var oType = oldTask.GetType(); foreach (var oProperty in oType.GetProperties()) { var oOldValue = oProperty.GetValue(oldTask, null); var oNewValue = oProperty.GetValue(newTask, null); if (Equals(oOldValue, oNewValue)) { continue; } if (oProperty.Name == "Changes" || oProperty.Name == "User") { continue; } string oldValue = "", newValue = ""; var diff = new TaskChanges { ModifiedOn = DateTime.Now, TaskId = newTask.Id }; if (oProperty.Name == "Categories") { oldValue = string.Join(", ", oldTask.Categories.Select(c => c.Category.Name)); if (oldValue == newCategories) { continue; } newValue = newCategories; } else { oldValue = oOldValue == null ? "" : oOldValue.ToString(); newValue = oNewValue == null ? "" : oNewValue.ToString(); } diff.Description = oProperty.Name + " was changed from \"" + oldValue + "\" to \"" + newValue + "\""; changes.Add(diff); } return(changes); }