public void Test_Vaildator_Error(string FieldValue, bool trg) { var _ServiceHandlerFactory = new TestServiceHandlerFactory(); var _Filter = new TestFilterFactory(); var _testRulesFactory = new TestRulesFactory(); var _common = new CommonInfo(); /* * * (IServiceHandlerFactory ServiceHandlerFactory * , IFilterFactory FilterFactory * , IRule RulesFactor * , ICommonInfo CommonInfo) * */ var testController = new TestController(_ServiceHandlerFactory , _Filter , _testRulesFactory , _common ); SetWebControllerInstance.Initialize(testController); var Model = default(IBaseViewModelRule); Model = new TestViewModel { Name = FieldValue }; if (FieldValue == string.Empty) { Model = new TestErrorViewModel { Name = FieldValue } } ; testController.Validate(Model); var isTrue = testController.ModelState .SelectMany(c => c.Value.Errors) .Any(); Assert.AreNotSame(isTrue.ToString(), trg.ToString(), "Validation Test Failed"); }
//[Test] public void rules_are_executed_with_not_vaild_rules() { ModelStateDictionary modelState = new ModelStateDictionary(); var _edit = new Moq.Mock <SubCategoryEdits>(_Context.Object); var x_scRepos = new Mock <EfRepository <SubCategory> >(_Context.Object) { CallBase = true }; x_scRepos.Setup(s => s.Add(It.IsAny <SubCategory>())); x_scRepos.Setup(s => s.ModifiedEntities).Returns(It.IsAny <EntityRules>()); //x_scRepos.Setup(s => // s.RunRules(It.IsAny<ServiceRuleFunc<bool>>(), It.IsAny<ModelStateDictionary>(), It.IsAny<object>())).Returns(true); var subCategoryEntity = new TestSubCategory { Id = 1 , SubCategoryName = new DomainEntityTypeString { Value = "Default" } }; subCategoryEntity.SubCategoryName.Value = "dd"; var _service = new Moq.Mock <SubCategoriesService>(); var testRulesFactory = new TestRulesFactory(); _service = new Moq.Mock <SubCategoriesService>(x_scRepos.Object , _edit.Object, testRulesFactory) { CallBase = true }; _service.Object.Add(subCategoryEntity); _service.Object.Verify(modelState); //SetEntitiesDefaults(It.IsAny<SubCategory>() _service.Verify(mock => _service.Object.RunRules(It.IsAny <ServiceRuleFunc <bool> >(), It.IsAny <ModelStateDictionary>(), It.IsAny <object>()), Times.Once); // SubCategoryRules // MockSubCategoryEdits.Verify(mock => mock.SetEntitiesDefaults(It.IsAny<SubCategory>()), Times.Once); // Assert.IsTrue(modelState.Count == 1, "Model Validation Rule didn't Failed."); }
//public void Test_Webapi_Parser_Attribute_IsInValid() //{ // string json3 = @" // { // 'CategoryId':{'value':'2'} // ,'CategoryTypeId':'2' // ,'subCategoryName':'Create Test' // ,'subCategoryTypeName':'Create Test234' // } // "; // var json = JObject.Parse(json3); // test_webapi_parser<SubCategoryViewModel>(json, false); //} // [Test] //public void test_webapi_parser_isValid() //{ // string json3= @" // { // 'CategoryId':{'value':'2'} // ,'CategoryTypeId':{'value':'2'} // ,'subCategoryName':{'value':'Create Test'} // ,'subCategoryTypeName':'Create Test234' // } // "; // var json = JObject.Parse(json3); // dynamic json4 = new JObject(); // json4.CategoryId = "2"; // // json4.CategoryId.Attributes = new JArray() as dynamic; // json4.CategoryTypeId = "2"; // json4.subCategoryName = "Create Test"; // json4.subCategoryTypeName = "Create Test234"; // //json4.err = "error"; // test_webapi_parser<SubCategoryViewModel>(json, false); //} // [Test] //public void test_webapi_parser_isInValid() //{ // dynamic json = new JObject(); // json.CategoryId = "2"; // json.CategoryTypeId = "2"; // json.subCategoryName = "Create Test"; // json.xsubCategoryTypeName = "Create Test234"; // test_webapi_parser<SubCategoryViewModel>(json,true); //} public void test_webapi_parser <T>(JObject json, bool bBoolean) where T : class, IDomainViewModel { var _ServiceHandlerFactory = new TestServiceHandlerFactory(); var _Filter = new TestFilterFactory(); var _testRulesFactory = new TestRulesFactory(); var _common = new CommonInfo(); var testController = new TestWebController(_ServiceHandlerFactory , _testRulesFactory , _Filter , _common ); SetWebControllerInstance.Initialize(testController); var res = testController.TestJsonParsing <T>(json); Assert.AreEqual(bBoolean, res, "webapi parser failed"); }
public void Test_SubCategoryControl_Create_Is_Not_Valid() { var dataSubCategories = new List <SubCategory> { new TestSubCategory { Id = 1, SubCategoryName = new DomainEntityTypeString { Value = "ABC" } }, new TestSubCategory { Id = 2, SubCategoryName = new DomainEntityTypeString { Value = "ZZZ" } }, new TestSubCategory { Id = 3, SubCategoryName = new DomainEntityTypeString { Value = "MMM" } }, }.AsQueryable(); G_mockSubCategoriesService.Setup(s => s.GetAll()).Returns(dataSubCategories); //var sc = new Moq.Mock<SubCategory>(); var sc = new TestSubCategory(); G_mockSubCategoriesService.Setup(s => s.CreateServiceEntity()).Returns(sc); var si = new Moq.Mock <SubCategoryClassItem>(); G_mockSubCategoryClassItemService.Setup(s => s.CreateServiceEntity()).Returns(si.Object); var _ServiceHandlerFactory = new TestServiceHandlerFactory(); var _Filter = new TestFilterFactory(); var _rule = new TestRulesFactory(); var common = new CommonInfo(); var controller = new SubCategoryController(G_mockCategoriesService.Object , G_mockSubCategoriesService.Object , G_mockSubCategoryTypesService.Object , G_mockSubCategoryItemsService.Object , _ServiceHandlerFactory , G_mockSubCategoryClassItemService.Object , _Filter , _rule , common , G_UnitOfWork ); SetWebControllerInstance.Initialize(controller); var res = controller.GetAvailableCategory(1, 1); dynamic attr = new ExpandoObject(); // attr.propList = new List<PropListKeyPair>(); // attr.Filter = new List<FilterKeyPair>(); var Model = new SubCategoryModel { SubCategoryName = new DomainEntityTypeString { Value = string.Empty, Attributes = null } , CategoryId = new DomainEntityTypeInt { Value = int.MaxValue , Attributes = attr } , CategoryTypeId = new DomainEntityTypeInt { Value = int.MaxValue } }; controller.Create(Model); var isTrue = controller.ModelState .SelectMany(c => c.Value.Errors) .Any(a => a.ErrorMessage.Contains("SubCategoryName")); Assert.IsTrue(!isTrue, "Failed to throw exception."); }
//[Test] public void edits_are_executed_() { Moq.Mock <SubCategoryEdits> _edit; Moq.Mock <SubCategoriesService> _service; /* * public SubCategoriesService(IRepository<SubCategory> subCategoryRepository * , IServiceEntityEdit<SubCategory> Edits * ) */ var context = new ReposContext("ReposContext"); _edit = new Moq.Mock <SubCategoryEdits>(_Context.Object); var scRepos = new EfRepository <SubCategory>(G_mockUnitOfWork.Object); //var _scRepos = new Mock<EfRepository<SubCategory>>(context); //.Object) { CallBase = true }; var sc = new TestSubCategory { Id = 1, SubCategoryName = new DomainEntityTypeString { Value = "Default" } }; var dataCategories = new List <Category> { new TestCategory { CategoryName = new DomainEntityTypeString { Value = "AAA" } }, new TestCategory { CategoryName = new DomainEntityTypeString { Value = "BBB" } }, new TestCategory { CategoryName = new DomainEntityTypeString { Value = "ZZZ" } } }.AsQueryable(); var mockSet = new Mock <DbSet <SubCategory> >() { CallBase = true }; var DB = new TestDbSet <SubCategory>(); // DbSet<SubCategory>(); mockSet.As <IQueryable <SubCategory> >().Setup(m => m.Provider).Returns(dataCategories.Provider); mockSet.As <IQueryable <SubCategory> >().Setup(m => m.Expression).Returns(dataCategories.Expression); mockSet.As <IQueryable <SubCategory> >().Setup(m => m.ElementType).Returns(dataCategories.ElementType); // mockSet.As<IQueryable<SubCategory>>().Setup(m => m.GetEnumerator()).Returns(o => dataCategories.GetEnumerator()); //_Context.Setup(m => m.Set<SubCategory>()).Returns(DB); var MockDbContext = EntityFrameworkMockHelper.GetMockContext <IDbContext>(); MockDbContext.CallBase = true; MockDbContext.Setup(m => m.Set <SubCategory>()).Returns(DB); var _scRepos = new Mock <IRepository <SubCategory> >() { CallBase = true }; // var mockSet = new Mock<DbSet<SubCategory>>() { CallBase = true }; var x_scRepos = new Mock <EfRepository <SubCategory> >(G_mockUnitOfWork.Object) { CallBase = true }; x_scRepos.Setup(s => s.Add(It.IsAny <SubCategory>())); x_scRepos.Setup(s => s.ModifiedEntities).Returns(It.IsAny <EntityRules>()); var scReposx = new EfRepository <SubCategory>(G_mockUnitOfWork.Object); var testRulesFactory = new TestRulesFactory(); _service = new Moq.Mock <SubCategoriesService>(x_scRepos.Object //_mockSubCategoryRepository.Object , _edit.Object, testRulesFactory) { CallBase = true }; var cat = EntitiesUtil.CreateEntity <SubCategory>(); _edit.Setup(s => s.SetEntitiesDefaults(cat)); var Model = new SubCategoryModel { SubCategoryName = new DomainEntityTypeString { Value = "Defaultx", Attributes = null } }; // _service.Object.CreateServiceEntity(); ModelStateDictionary modelState = new ModelStateDictionary(); // _service.Object.Save(); var subCategoryEntity = new TestSubCategory { Id = 1 , SubCategoryName = new DomainEntityTypeString { Value = "Default" } }; _service.Object.Add(subCategoryEntity); _service.Object.Verify(modelState); // _edit.Object.SetEntitiesDefaults(cat); // _edit.Verify(s => s.SetEntitiesDefaults(cat), Times.Once); }