ekhidna
ekhidna
CC#
Created by ekhidna on 7/2/2024 in #help
Confusing code with keyword ref
I am refactoring an old code base and I stumbled on this:
public interface IBaseRepository<TEntity> {
bool InsertNanoEntity(TEntity entity, string login = null);
bool InsertNanoEntity(TEntity entity, ref AuditDto audit, string login = null);
}

public abstract class BaseRepository<TEntity> : IDisposable, IBaseRepository<TEntity> where TEntity : NanoBaseEntity {

public bool InsertNanoEntity(TEntity entity, string login = null) {
AuditDto audit = null;
return this.InsertNanoEntity(entity, ref audit, login);
}

public bool InsertNanoEntity(TEntity entity, ref AuditDto audit, string login = null) {

if (this.LoggedUser != null) {
entity.SiteId = this.LoggedUser.GetUserSessionSiteId;
}

entity.ModifiedDate = DateTime.Now;
EntityEntry dbEntity = this._context.Entry(entity);
dbEntity.State = EntityState.Added;

if (audit != null) {
audit.AuditList.AddRange(this.CreateAuditTrace(dbEntity, AuditTypeEnumDto.Create));
}
bool success = this._context.SaveChanges() != 0;
return success;
}
public interface IBaseRepository<TEntity> {
bool InsertNanoEntity(TEntity entity, string login = null);
bool InsertNanoEntity(TEntity entity, ref AuditDto audit, string login = null);
}

public abstract class BaseRepository<TEntity> : IDisposable, IBaseRepository<TEntity> where TEntity : NanoBaseEntity {

public bool InsertNanoEntity(TEntity entity, string login = null) {
AuditDto audit = null;
return this.InsertNanoEntity(entity, ref audit, login);
}

public bool InsertNanoEntity(TEntity entity, ref AuditDto audit, string login = null) {

if (this.LoggedUser != null) {
entity.SiteId = this.LoggedUser.GetUserSessionSiteId;
}

entity.ModifiedDate = DateTime.Now;
EntityEntry dbEntity = this._context.Entry(entity);
dbEntity.State = EntityState.Added;

if (audit != null) {
audit.AuditList.AddRange(this.CreateAuditTrace(dbEntity, AuditTypeEnumDto.Create));
}
bool success = this._context.SaveChanges() != 0;
return success;
}
Problem number 1. The 2nd method it's called only by the 1st one. So I can remove it from interface, and make it private. But my great doubt is with the ref AuditDto audit parameter. Before being called there's always that code line: AuditDto audit = null; Can I safely remove the ref AuditDto audit parameter and convert the method to Async? Or because it's a parameter called by reference it will change audit status and I should not do that. I understand that calling by reference will change the object status outside the method. My problem is that before being called it is always set to null. So my final question is can I make something like:
public async Task<bool> InsertNanoEntityAsync(TEntity entity, string login = null) {
....
}
public async Task<bool> InsertNanoEntityAsync(TEntity entity, string login = null) {
....
}
Or, besides being called always as null, should I still worry with audit ? Thank you !!!
11 replies
CC#
Created by ekhidna on 4/9/2024 in #help
Unit Testing with NUnit
Hello everyone. I got this service that is called by a controller and gets data in the Db.
public async Task<IEnumerable<ApplicationUser>> GetAllUsersAsync() {

List<ApplicationUser> users = await _userManager.Users.ToListAsync();
return users;
}
public async Task<IEnumerable<ApplicationUser>> GetAllUsersAsync() {

List<ApplicationUser> users = await _userManager.Users.ToListAsync();
return users;
}
Does this unit test, test anything? or because everything is Mocked it actually tests nothing?
[TestFixture]
public class UserServiceTests {
Mock<IUserService>? _userService;

[SetUp]
public void Setup() {
_userService = new Mock<IUserService>();
}

[Test]
public async Task Test_UserService() {
Guid id = Guid.NewGuid();
;
IEnumerable<ApplicationUser> app = new List<ApplicationUser>()
{
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser1", Name = "User1", Email = "[email protected]"},
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser2", Name = "User2", Email = "[email protected]"},
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser3", Name = "User2", Email = "[email protected]"},
};

_userService = new Mock<IUserService>(MockBehavior.Strict);
_userService.Setup(x => x.GetAllUsersAsync()).ReturnsAsync(app);
var users = await _userService.Object.GetAllUsersAsync();
Assert.That(users.Count(), Is.EqualTo(3));

}
}
[TestFixture]
public class UserServiceTests {
Mock<IUserService>? _userService;

[SetUp]
public void Setup() {
_userService = new Mock<IUserService>();
}

[Test]
public async Task Test_UserService() {
Guid id = Guid.NewGuid();
;
IEnumerable<ApplicationUser> app = new List<ApplicationUser>()
{
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser1", Name = "User1", Email = "[email protected]"},
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser2", Name = "User2", Email = "[email protected]"},
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser3", Name = "User2", Email = "[email protected]"},
};

_userService = new Mock<IUserService>(MockBehavior.Strict);
_userService.Setup(x => x.GetAllUsersAsync()).ReturnsAsync(app);
var users = await _userService.Object.GetAllUsersAsync();
Assert.That(users.Count(), Is.EqualTo(3));

}
}
Because the method GetAllUsersAsync appears green as if it was tested. But if I Debug the test it never goes into the function in the code. I guess that's because everything is mocked. Not sure. Some help here. Thank you
7 replies