示例#1
0
        public void CompanyFacadeTest()
        {
            var unit = new UnitOfWork(GetInMemoryOptions());

            Seeder.Seed(unit);

            var userService   = new UserService(unit, new UserQueryObject(unit));
            var compService   = new CompanyService(unit, new CompanyQueryObject(unit));
            var companyFacade = new CompanyFacade(unit, mapper, compService, userService);

            // Null ID edit/update
            companyFacade.AddAsync(new CompanyDto()
            {
                Name = "Lol"
            }).Wait();
            //unit.SaveChanges();
            var lol = compService.ListCompaniesByNameAsync("Lol").Result.First();

            Assert.NotNull(lol);
            Assert.NotNull(lol.Id);
            lol.Id   = null;
            lol.Name = "new lol";
            var excp2 = Assert.Throws <AggregateException>(() =>
                                                           companyFacade.EditInfoAsync(mapper.Map <CompanyDto>(lol)).Wait());

            Assert.Contains("The property 'Id' on entity type 'Company' is part of a key " +
                            "and so cannot be modified or marked as modified.",
                            excp2.Message);


            // Addition with conflicting ID
            // This invalidates the database
            var id1Company = unit.CompanyRepository.GetById(1);

            Assert.NotNull(id1Company); // makes sure company with id 1 is already in database
            var excp = Assert.Throws <AggregateException>(() =>
                                                          companyFacade.AddAsync(new CompanyDto()
            {
                Id = 1
            }).Wait());

            Assert.Contains("The instance of entity type 'Company' cannot be tracked " +
                            "because another instance with the same key value for {'Id'} is already being tracked.",
                            excp.Message);

            unit.Dispose();
        }
示例#2
0
        public async Task <IActionResult> AddCompany(CompanyDto company)
        {
            if (ModelState.IsValid)
            {
                await companyFacade.AddAsync(company);

                return(RedirectToAction("Login", "User"));
            }

            throw new ArgumentException();
        }