mercredi 29 avril 2020

Testing EFCore using XUnit and Moq in ASP.NET Core

I am a newbie in testing and i am writing my first lines of code to test the GetById in EFCore layer, i wrote this code to instanciate the service :

public class GroupTest
    {
        private readonly GroupService<GroupDTO, Group, GroupUser, AuthContext> _service;
        private readonly Mock<IUserRoleRepository<UserRole>> _userRoleRepo = new Mock<IUserRoleRepository<UserRole>>();
        private readonly Mock<IGroupUserRepository<GroupUser>> _groupUserRepo = new Mock<IGroupUserRepository<GroupUser>>();
        private readonly Mock<IGroupRepository<Group>> _groupRepo = new Mock<IGroupRepository<Group>>();
        private readonly Mock<ICurrentContextProvider> _contextProvider = new Mock<ICurrentContextProvider>();
        private readonly Mock<AuthContext> dbContext = new Mock<AuthContext>();


        public GroupTest()
        {
            _service = new GroupService<GroupDTO, Group, GroupUser, AuthContext>
                (_contextProvider.Object,_groupRepo.Object, _userRoleRepo.Object, _groupUserRepo.Object,dbContext.Object);

        }

Then i wrote my first testcase :

[Fact]
public async Task GetByIdAsync_ShouldReturnGroup_WhenGroupExists()
{
  const int id = 1;
  var resultGroup = await _service.GetById(id);
  Assert.Equal(id, resultGroup.Id);
}

When i excuted the test an exception is thrown : enter image description here

Any suggestion please?

Aucun commentaire:

Enregistrer un commentaire