Skip to content

Commit

Permalink
#24: Implement CreateApplicant unit test for good inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
choutianxius committed Sep 13, 2024
1 parent 4c13401 commit fc6ac7a
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions AggieRent.Tests/Services/ApplicantServiceTest.cs
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;

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit fc6ac7a

Please sign in to comment.