public void Edit_Post_Test_InvalidModelState() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Name = "mmm", Id = 1, AddedOrEditedAttr = new ContentAttributeModel//todo: not exist { Id = 1, Name = "attr mmm" } }; controller.ModelState.AddModelError("Name", "Required"); controller.Edit(model); mockRepo.Verify(db => db.SaveChanges(), Times.Never); Assert.Equal("Test name", mockRepo.Object.ContentTypes.First().Name); Assert.Equal("Attr1", mockRepo.Object.ContentTypes.First().Attrs.First().Name); }
public void AddDictionaryAttributeTest_DictionaryNotFound() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.ContentAttrs, GetAttributeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Id = 1, Name = "Test name", AddedOrEditedAttr = new ContentAttributeModel { Id = null, //it means that's a new attribute Name = "Sex", AttributeType = AttrType.Dictionary, DictionaryId = 11 } }; controller.ModelState.AddModelError("Name", "Required"); Assert.Single(mockRepo.Object.ContentTypes.First().Attrs); var result = controller.Edit(model); Assert.IsType <NotFoundObjectResult>(result); var r = (NotFoundObjectResult)result; Assert.Equal($"Dictionary with id = { model.AddedOrEditedAttr.DictionaryId } not found", r.Value); }
public void Edit_Post_Test() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Name = "mmm", Id = 1, AddedOrEditedAttr = new ContentAttributeModel { Id = 1, Name = "attr mmm", Required = true, AttributeType = AttrType.Image } }; var result = controller.Edit(model); var viewResult = Assert.IsType <ViewResult>(result); mockRepo.Verify(db => db.SaveChanges(), Times.Once()); Assert.Equal("mmm", mockRepo.Object.ContentTypes.First().Name); Assert.Equal("attr mmm", mockRepo.Object.ContentTypes.First().Attrs.First().Name); var resultModel = Assert.IsAssignableFrom <EditContentTypeModel>( viewResult.ViewData.Model); Assert.Equal("mmm", resultModel.Name); Assert.Equal("attr mmm", resultModel.Attrs.First().Name); Assert.True(resultModel.Attrs.First().Required); Assert.Equal(AttrType.Image, resultModel.Attrs.First().AttributeType); }
public void Edit_Test_Get_EditAttr_NotFound() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentTypeController(mockRepo.Object); var result = controller.Edit(1, 11); Assert.IsType <NotFoundResult>(result); }
public void Edit_Post_Test_InvalidContentTypeId() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Name = "mmm", Id = 11, }; var result = controller.Edit(model); Assert.IsType <NotFoundResult>(result); }
public void Edit_Test_Get() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var result = controller.Edit(1, null); var viewResult = Assert.IsType <ViewResult>(result); var resultModel = Assert.IsAssignableFrom <EditContentTypeModel>( viewResult.ViewData.Model); Assert.Single(resultModel.Attrs); Assert.Equal("Attr1", resultModel.Attrs.First().Name); Assert.Equal(1, resultModel.Id); Assert.Equal("Test name", resultModel.Name); Assert.Null(resultModel.AddedOrEditedAttr); }
public void Edit_Post_Test_AddAttribute() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Id = 1, AddedOrEditedAttr = new ContentAttributeModel//todo: not exist { Name = "attr mmm", Required = true, AttributeType = AttrType.Image } }; controller.ModelState.AddModelError("Name", "Required"); Assert.Single(controller.ModelState); Assert.Single(mockRepo.Object.ContentTypes.First().Attrs); var result = controller.Edit(model); var viewResult = Assert.IsType <ViewResult>(result); var resultModel = Assert.IsAssignableFrom <EditContentTypeModel>( viewResult.ViewData.Model); mockRepo.Verify(db => db.SaveChanges(), Times.Once); Assert.Empty(controller.ModelState); Assert.Equal(2, mockRepo.Object.ContentTypes.First().Attrs.Count); Assert.Equal(2, resultModel.Attrs.Count); var attrs = mockRepo.Object.ContentTypes.First().Attrs.Where(a => a.Name == "attr mmm"); Assert.Single(attrs); Assert.True(attrs.First().Required); Assert.Equal(AttrType.Image, attrs.First().AttrType); }
public void Edit_Post_Test_InvalidAttrId() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Name = "mmm", Id = 1, AddedOrEditedAttr = new ContentAttributeModel//todo: not exist { Id = 11, Name = "attr mmm" } }; Assert.Throws <InvalidOperationException>(() => controller.Edit(model)); mockRepo.Verify(db => db.SaveChanges(), Times.Never); }
public void Edit_Post_Test_AddAttribute_ContentTypeNotFound() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Id = 11, AddedOrEditedAttr = new ContentAttributeModel//todo: not exist { Name = "attr mmm" } }; controller.ModelState.AddModelError("Name", "Required"); var result = controller.Edit(model); Assert.IsType <NotFoundResult>(result); }
public void AddDictionaryAttributeTest() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.ContentAttrs, GetAttributeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Id = 1, Name = "Test name", AddedOrEditedAttr = new ContentAttributeModel { Id = null, //it means that's a new attribute Name = "Sex", AttributeType = AttrType.Dictionary, DictionaryId = 1 } }; controller.ModelState.AddModelError("Name", "Required"); Assert.Single(mockRepo.Object.ContentTypes.First().Attrs); var result = controller.Edit(model); Assert.Equal(2, mockRepo.Object.ContentTypes.First().Attrs.Count); Assert.IsType <ViewResult>(result); mockRepo.Verify(db => db.SaveChanges(), Times.Once()); var attr = mockRepo.Object.ContentTypes.First().Attrs.Last(); Assert.Equal("Sex", attr.Name); Assert.Equal(AttrType.Dictionary, attr.AttrType); Assert.Equal(mockRepo.Object.Dictionaries.First().Id, attr.DictionaryId); }