C#C
C#12mo ago
surwren

✅ Mocking DB operations

Say I have a function and I need to verify that the saved UserFollow object will:

  1. Cause the user objects to be retrieved if UserFollow retrieves them eagerly
  2. Cause the userFollow to be retrieved if a User is retrieved with said fields eagerly
public async Task<bool> AddFollowerByPrivateIdAsync(int userPrivateId, int followerPrivateId) { 

var user = await _context.Users.FirstOrDefaultAsync(u => u.PrivateId == userPrivateId);

var follower = await _context.Users.FirstOrDefaultAsync(u => u.PrivateId == followerPrivateId);

if (user == null || follower == null) {

return false;

}

var userFollow = new UserFollow {

FollowedId =
[
user.Id
]
FollowerId =
[
follower.Id
]
Type = FollowType.Requested,

Followed = user,

Follower = follower

};

_context.UserFollows.Add(userFollow);

await _context.SaveChangesAsync();

return true;

}


How would I test this? I have looked at XUnit and MockItEasy but it doesn't look like they are dealing with the Database, but rather dependencies for abstracted code.
Was this page helpful?
✅ Mocking DB operations - C#