Example #1
0
        public Result Save(UserSaveArguments args)
        {
            if (!ModelState.IsValid)
            {
                return(ModelState.AllErrors());
            }
            if (this.Exists(args.Username, args.Id))
            {
                return("用户名已存在。");
            }

            var user = args.CopyTo <User>();

            if (!args.Id.HasValue)
            {
                user.Id = Guid.NewGuid();
                this.Bus.Add(user);
                return(new Result <Guid>(user.Id));
            }
            else
            {
                this.Bus.Modify(user);
                return(Result.Successfully);
            }
        }
        public void Post_Save_Test()
        {
            var isExists = true;
            var args     = new UserSaveArguments()
            {
                Id       = Guid.NewGuid(),
                Username = "******",
                Password = "******",
                RealName = "test",
            };

            var c = WebConfig.Mocks.Create <AccountController>(f => f
                                                               .Mock <CMD.ExistsWhere <User> >((context, command) =>
            {
                Assert.Equal("abc", command.WhereParameters.Parameters["Username"].Value);
                Assert.Equal(args.Id.Value, command.WhereParameters.Parameters["Id"].Value);
                command.ResultValue = isExists;
            })
                                                               .Mock <CMD.Exists <User> >((context, command) =>
            {
                Assert.Equal("Username", command.KeyName);
                Assert.Equal("abc", command.KeyValue);
                command.ResultValue = isExists;
            })
                                                               .Mock <CMD.Modify <User> >((context, command) =>
            {
                command.Entity.AssertObject(args.CopyTo <User>());
                command.ResultValue = 1;
            })
                                                               .Mock <CMD.Add <User> >((context, command) =>
            {
                command.Entity.AssertObject(args.CopyTo <User>());
                command.ResultValue = 1;
            }));

            Assert.Equal("用户名已存在。", c.Save(args).Message);
            args.Id = null;
            Assert.Equal("用户名已存在。", c.Save(args).Message);
            args.Id  = Guid.NewGuid();
            isExists = false;
            c.Save(args).ThrowIfFailded();
            args.Id = null;
            Assert.NotNull((c.Save(args) as Result <Guid>));
        }