public async Task CreateAsync_WithValidModel_ShouldReturnTrue_And_InsertInDatabase() { // Arrange var dbCount = this.dbContext.Accounts.Count(); await this.SeedUserAsync(); var model = new CardCreateServiceModel { Name = SampleName, UserId = SampleUserId, AccountId = SampleAccountId, ExpiryDate = SampleExpiryDate }; // Act var result = await this.cardService.CreateAsync(model); // Assert result .Should() .BeTrue(); this.dbContext .Cards .Should() .HaveCount(dbCount + 1); }
public async Task <bool> CreateAsync(CardCreateServiceModel model) { if (!this.IsEntityStateValid(model) || !this.Context.Users.Any(u => u.Id == model.UserId)) { return(false); } string generatedNumber; string generated3DigitSecurityCode; do { generatedNumber = this.cardHelper.Generate16DigitNumber(); generated3DigitSecurityCode = this.cardHelper.Generate3DigitSecurityCode(); } while (await this.Context.Cards.AnyAsync(a => a.Number == generatedNumber && a.SecurityCode == generated3DigitSecurityCode)); var dbModel = Mapper.Map <Card>(model); dbModel.Number = generatedNumber; dbModel.SecurityCode = generated3DigitSecurityCode; await this.Context.Cards.AddAsync(dbModel); await this.Context.SaveChangesAsync(); return(true); }
public async Task CreateAsync_WithInvalidExpiryDate_Should_ReturnFalse(string expiryDate) { // Set invalid expiryDate var model = new CardCreateServiceModel { Name = SampleName, UserId = SampleUserId, AccountId = SampleAccountId, ExpiryDate = expiryDate }; // Act var result = await this.cardService.CreateAsync(model); // Assert result .Should() .BeFalse(); }
public async Task CreateAsync_WithInvalidName_Should_ReturnFalse() { // Set invalid name var model = new CardCreateServiceModel { Name = new string('m', ModelConstants.Card.NameMaxLength + 1), UserId = SampleUserId, AccountId = SampleAccountId, ExpiryDate = SampleExpiryDate }; // Act var result = await this.cardService.CreateAsync(model); // Assert result .Should() .BeFalse(); }
public async Task CreateAsync_WithInvalidExpiryDate_Should_Not_InsertInDatabase(string expiryDate) { // Set invalid expiryDate var model = new CardCreateServiceModel { Name = SampleName, UserId = SampleUserId, AccountId = SampleAccountId, ExpiryDate = expiryDate }; // Act await this.cardService.CreateAsync(model); // Assert this.dbContext .Cards .Should() .BeEmpty(); }
public async Task CreateAsync_WithInvalidName_Should_Not_InsertInDatabase() { // Set invalid name var model = new CardCreateServiceModel { Name = new string('m', ModelConstants.Card.NameMaxLength + 1), UserId = SampleUserId, AccountId = SampleAccountId, ExpiryDate = SampleExpiryDate }; // Act await this.cardService.CreateAsync(model); // Assert this.dbContext .Cards .Should() .BeEmpty(); }
public async Task CreateAsync_WithValidModel_Should_ReturnTrue() { // Arrange await this.SeedUserAsync(); var model = new CardCreateServiceModel { Name = SampleName, UserId = SampleUserId, AccountId = SampleAccountId, ExpiryDate = SampleExpiryDate }; // Act var result = await this.cardService.CreateAsync(model); // Assert result .Should() .BeTrue(); }