xUnit test EF throws an exception c#
During the test, two EF methods throw an exception.I kind of made a mock object. What is the problem?
Command Test
public class CommandUserTests
{
private readonly Mock<IStoreDbContext> _context;
private readonly Mock<IUserStore<User>> _userManager;
public CommandUserTests()
{
_context = new Mock<IStoreDbContext>();
_userManager = new Mock<IUserStore<User>>();
}
[Fact]
public async Task CreateUserHandler_Success()
{
//arrange
var fixture = new CreateUser.Command("pavel", "[email protected]", "papsdadk21xi", "Poland", "+3753391822183");
var handler = new CreateUser.Handler(_context.Object,_userManager.Object);
//act
var result = await handler.Handle(fixture, new CancellationToken());
}
Command
public static class CreateUser
{
public record Command(string UserName, string Email, string Password, string Country,
string PhoneNumber) : IRequest<Guid>;
public class Handler : IRequestHandler<Command, Guid>
{
private readonly IStoreDbContext _context;
private readonly IUserStore<User> _userManager;
public Handler(IStoreDbContext context, IUserStore<User> userManager)
{
_context = context;
_userManager = userManager;
}
public async Task<Guid> Handle(Command request, CancellationToken cancellationToken)
{
var Country =
await _context.Countries.FirstOrDefaultAsync(c => c.Name == request.Country, cancellationToken);//throw exception
//
var user = new User
{
Id = Guid.NewGuid(),
UserName = request.UserName,
Email = request.Email,
PasswordHash = request.Password,
Country = Country,
CountryId = Country.Id,
PhoneNumber = request.PhoneNumber,
CreateDate = DateTime.Now
};
var users = new List<User>();
users.Add(user);
Country.Users = users;
_context.Countries.Update(Country);//throw exception
//
await _userManager.CreateAsync(user,cancellationToken);
await _context.SaveChangesAsync(cancellationToken);
return user.Id;
}
}
}