Example #1
0
        public void Initialize()
        {
            //Initialize_data
            User user1 = new User() { Id = 1, Email = "123", CreatedTime = DateTime.Now, Name = "user1", NickName = "梁贵", Password = "123", };
            User user2 = new User() { Id = 2, Email = "123", CreatedTime = DateTime.Now, Name = "user2", NickName = "梁贵2", Password = "123", };
            _users = new List<User> { user1, user2 };

            Role role1 = new Role() { Id = 1, Name = "role1", Remark = "role1" };
            Role role2 = new Role() { Id = 2, Name = "role2", Remark = "role2" };
            _roles = new List<Role> { role1, role2 };
            user1.Roles = _roles;

            //Initialize_interface
            _unitOfWork = Substitute.For<IUnitOfWork>();
            _identityService = Substitute.For<IdentityService>(_unitOfWork);
            _userRepository = Substitute.For<IRepository<User, int>>();
            _roleRepository = Substitute.For<IRepository<Role, int>>();
            _userRepository.GetByKey(Arg.Any<int>()).ReturnsForAnyArgs(x => _users.Find(r => r.Id == (int)x[0]));
            _userRepository.Entities.Returns(_users.AsQueryable());

            _roleRepository.Entities.Returns(_roles.AsQueryable());
            _roleRepository.GetByKey(Arg.Any<int>()).Returns(x => _roles.Find(r => r.Id == (int)x[0]));
            _identityService.UserRepository.Returns(_userRepository);
            _identityService.RoleRepository.Returns(_roleRepository);
        }
Example #2
0
        public static EmailTemplate LoadTemplate(IEmailContext context, IRepository<EmailTemplate> emailTemplateRepository)
        {
            try
            {
                EmailTemplate template = null;
                if (context.TemplateId.HasValue)
                {
                    var repository = (EmailTemplateRepository)emailTemplateRepository;
                    template = repository.GetById(context.TemplateId);
                }
                else
                {
                    template = emailTemplateRepository.GetByKey(context.TemplateKey);
                }

                if (template == null)
                {
                    throw new EmailTemplateNotFoundException(context.TemplateKey);
                }
                return template;
            }
            catch (ItemNotFoundException)
            {
                throw new EmailTemplateNotFoundException(context.TemplateKey);
            }
        }