-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#24: Implement CreateApplicant unit test for good inputs
- Loading branch information
1 parent
4c13401
commit fc6ac7a
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
using AggieRent.DataAccess; | ||
using AggieRent.Models; | ||
using AggieRent.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Moq; | ||
using Xunit; | ||
|
||
|
@@ -89,4 +90,64 @@ public void GetApplicants_ThenOk() | |
mockApplicantRepository.Verify(x => x.GetAll(), Times.Once()); | ||
} | ||
} | ||
|
||
public class ApplicantService_CreateApplicantShould | ||
{ | ||
[Fact] | ||
public void CreateApplicant_GoodInput_ThenAddApplicantAndReturnId() | ||
{ | ||
var mockApplicantRepository = new Mock<IApplicantRepository>(); | ||
List<Applicant> applicants = | ||
[ | ||
new() | ||
{ | ||
Id = Guid.NewGuid().ToString(), | ||
Email = "[email protected]", | ||
HashedPassword = BC.HashPassword("veryStr0ngP@ssw0rd"), | ||
FirstName = "John", | ||
LastName = "Doe", | ||
}, | ||
]; | ||
mockApplicantRepository.Setup(x => x.GetAll()).Returns(applicants.AsQueryable()); | ||
mockApplicantRepository | ||
.Setup(x => x.Add(It.IsAny<Applicant>())) | ||
.Callback( | ||
(Applicant a) => | ||
{ | ||
if (applicants.FirstOrDefault(a1 => a1.Id == a.Id) != null) | ||
throw new Exception("Duplicate ID"); | ||
applicants.Add(a); | ||
} | ||
); | ||
var applicantService = new ApplicantService(mockApplicantRepository.Object); | ||
|
||
var email = "[email protected]"; | ||
var password = "superStr0ngp@ssw0rd"; | ||
var firstName = "John"; | ||
var lastName = "Deer"; | ||
var gender = Gender.Female; | ||
var birthday = new DateOnly(2000, 1, 1); | ||
var description = "Hi I'm John Deer"; | ||
var id = applicantService.CreateApplicant( | ||
email, | ||
password, | ||
firstName, | ||
lastName, | ||
gender, | ||
birthday, | ||
description | ||
); | ||
|
||
Assert.Equal(2, applicants.Count); | ||
var createdApplicant = applicants.FirstOrDefault(a => a.Id == id); | ||
Assert.NotNull(createdApplicant); | ||
Assert.Equal(email, createdApplicant.Email); | ||
Assert.True(BC.Verify(password, createdApplicant.HashedPassword)); | ||
Assert.Equal(firstName, createdApplicant.FirstName); | ||
Assert.Equal(lastName, createdApplicant.LastName); | ||
Assert.Equal(gender, createdApplicant.Gender); | ||
Assert.Equivalent(new DateOnly(2000, 1, 1), createdApplicant.Birthday); | ||
Assert.Equal(description, createdApplicant.Description); | ||
} | ||
} | ||
} |