How to moq Entity Framework SaveChangesAsync? - c#

Mock<IDbContext> dbContext;
[TestFixtureSetUp]
public void SetupDbContext()
{
dbContext = new Mock<IDbContext>();
dbContext.Setup(c => c.SaveChanges()).Verifiable();
dbContext.Setup(c => c.SaveChangesAsync()).Verifiable();
dbContext.Setup(c => c.Customers.Add(It.IsAny<Customer>()))
.Returns(It.IsAny<Customer>()).Verifiable();
}
[Test]
public async Task AddCustomerAsync()
{
//Arrange
var repository = new EntityFrameworkRepository(dbContext.Object);
var customer = new Customer() { FirstName = "Larry", LastName = "Hughes" };
//Act
await repository.AddCustomerAsync(customer);
//Assert
dbContext.Verify(c => c.Customers.Add(It.IsAny<Customer>()));
dbContext.Verify(c => c.SaveChangesAsync());
}
[Test]
public void AddCustomer()
{
//Arrange
var repository = new EntityFrameworkRepository(dbContext.Object);
var customer = new Customer() { FirstName = "Larry", LastName = "Hughes" };
//Act
repository.AddCustomer(customer);
//Assert
dbContext.Verify(c => c.Customers.Add(It.IsAny<Customer>()));
dbContext.Verify(c => c.SaveChanges());
}
And here's what I want to test:
public class EntityFrameworkRepository
{
private readonly IDbContext DBContext;
public EntityFrameworkRepository(IDbContext context)
{
DBContext = context;
}
public async Task AddCustomerAsync(Customer customer)
{
DBContext.Customers.Add(customer);
await DBContext.SaveChangesAsync();
}
public void AddCustomer(Customer customer)
{
DBContext.Customers.Add(customer);
DBContext.SaveChanges();
}
}
AddCustomers test passes.
AddCustomersAsync test fails, I keep getting a NullReferenceException after calling await DbContext.SaveChangesAsync().
at
MasonOgCRM.DataAccess.EF.EntityFrameworkRepository.d__2.MoveNext()
in
C:\Users\Mason\Desktop\Repositories\masonogcrm\src\DataAccess.EFRepository\EntityFrameworkRepository.cs:line
43
I can't see anything that's null in my code. DbContext is not null. The equivalent test of AddCustomers which is identical with the exception of not being async runs as expected. I suspect I haven't performed a correct setup of SaveChangesAsync in SetupDBContext() but I don't know what to do to fix it.

You are right the problem occurs because one of your setups incorrect :: dbContext.Setup(c => c.SaveChangesAsync()).Verifiable();.
The method return a Task and you forgot to return a Task therefore null returns.
You can remove dbContext.Setup(c => c.SaveChangesAsync()).Verifiable(); or
change the setup to something like:
dbContext.Setup(c => c.SaveChangesAsync()).Returns(() => Task.Run(() =>{})).Verifiable();

You could use
dataContext.Setup(x => x.SaveChangesAsync()).ReturnsAsync(1);
and
dataContext.Verify(x=>x.SaveChangesAsync());

This worked for me:
dbContext.Verify(m => m.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once());

It may also require a cancellation token, so something like this should work:
dataContext.Setup(x => x.SaveChangesAsync(It.IsAny<CancellationToken>())).ReturnsAsync(1);

Related

How can I return Task<bool> from a Mock method using Xunit and Moq

I have this Repository class
public class ComplaintRepository : IComplaintRepository
{
private readonly IAppDbContext _context;
public ComplaintRepository(IAppDbContext context)
{
_context = context;
}
public async Task<bool> AddAsync(Complaint complaint, CancellationToken token)
{
await Task.Run(() => _context.Complaints.AddAsync(complaint));
return await SaveAsync(token);
}
private async Task<bool> SaveAsync(CancellationToken token)
{
if (await _context.SaveChangesAsync(token) > 0)
return true;
else
return false;
}
I want to set up a test for the class using xUnit and Moq. When I mock the behavior of the AddAsync method like so:
public class ComplaintRepositoryTests
{
[Fact]
public async void AddAsync_WithValidComplaint_ShouldReturnTrue()
{
// Arrange
var mockContext = new Mock<IAppDbContext>();
mockContext.Setup(x => x.Complaints.AddAsync(It.IsAny<Complaint>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(true));
mockContext.Setup(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(1);
var complaintRepository = new ComplaintRepository(mockContext.Object);
var complaint = new Complaint();
// Act
var result = await complaintRepository.AddAsync(complaint, CancellationToken.None);
// Assert
Assert.True(result);
mockContext.Verify(x => x.Complaints.AddAsync(It.IsAny<Complaint>(), It.IsAny<CancellationToken>()), Times.Once());
mockContext.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once());
}
}
I am getting an error for the return type of the AddAsync method.
this is the error I am getting:
cannot convert from 'System.Threading.Tasks.Task<bool>' to 'System.Threading.Tasks.ValueTask<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<CCMP.Domain.Complaints.Complaint>>'
I don't know how to go about it. I have tried all the things I have seen online to no avail.

Moq doesn't verify actions on DbSet and DbContext after adding new data

I have the following xunit test using Moq
[Fact]
public async void Add_Valid()
{
// Arrange
var mockSet = new Mock<DbSet<CategoryDao>>();
var mockContext = new Mock<Data.Context.AppContext>();
mockContext.Setup(m => m.Categories).Returns(mockSet.Object);
var categoryProfile = new CategoryVoProfile();
var configMapper = new MapperConfiguration(cfg => cfg.AddProfile(categoryProfile));
IMapper mapper = new Mapper(configMapper);
var service = new InDbCategoryService(mockContext.Object, mapper);
// Act
await service.Add(new CategoryVo() { Name = "CreatedName1" });
// Assert
mockSet.Verify(m => m.Add(It.IsAny<CategoryDao>()), Times.Once()); // DbSet verification
mockContext.Verify(m => m.SaveChanges(), Times.Once()); // DbContext verification
}
And it throws this error:
Moq.MockException:
Expected invocation on the mock once, but was 0 times: m => m.Add(It.IsAny())
Performed invocations:
Mock<DbSet:1> (m):
No invocations performed.
When I delete the DbSet verification line and ask to verify only the DbContext, it throws this:
Moq.MockException :
Expected invocation on the mock once, but was 0 times: m => m.SaveChanges()
Performed invocations:
MockAppContext:1 (m):
AppContext.Categories = InternalDbSet
DbContext.Add(CategoryDao)
DbContext.SaveChangesAsync(CancellationToken)
The simplified service looks like this:
public class InDbCategoryService : IDataServiceAsync<CategoryVo>
{
private readonly Data.Context.AppContext context;
private readonly IMapper mapper;
public InDbCategoryService(Data.Context.AppContext context, IMapper mapper)
{
this.context = context;
this.mapper = mapper;
}
public async Task Add(CategoryVo item)
{
context.Add(entity: mapper.Map<CategoryDao>(item));
await context.SaveChangesAsync();
}
}
The category profile:
public class CategoryVoProfile : Profile
{
public CategoryVoProfile()
{
CreateMap<CategoryDao, CategoryVo>()
.ReverseMap();
}
}
Database context:
public class AppContext : DbContext
{
public AppContext() { }
public AppContext (DbContextOptions<AppContext> options) : base(options) { }
public virtual DbSet<CategoryDao> Categories { get; set; }
}
I've used this microsoft docs example for my test, but it's clear I'm missing something. Any help or advice is appreciated.
You are not testing the methods that you've called in your service. Your add method:
public async Task Add(CategoryVo item)
{
context.Add(entity: mapper.Map<CategoryDao>(item));
await context.SaveChangesAsync();
}
You'll note you're calling the DbContext.Add not the context.Categories.Add which is what you verify in your test:
mockSet.Verify(m => m.Add(It.IsAny<CategoryDao>()), Times.Once());
The same is true for your SaveChanges. You're verifying the synchronous version but calling the async one. So you need to modify what you're verifying to match what you're using.

C# TDD, Integration Test Repository, Verify Add Test

I'm relatively new to TDD and I have a project with repository pattern with EF in the repository class.
Now I want to test these repositories and I have already mocked the DbContext successfully. (Using Moq and nunit)
Now I want to validate adding a object to my mocked database. The test says, test passed, but I'm quite sure, my test is not correct because I queried the database before the Add method, returns 5 objects, then I call the Add method and query the database again, still only 5 objects, but I would expect now 6 objects. Am I missing here something?
Model Address.cs
public class Address
{
public int Id { get; set; }
public string Person {get; set; }
//some more properties
}
Repository.cs
public async Task<AdresseDto> AddAdresseAsync(AdresseDto adresseDto)
{
try
{
ctx.Adresse.Add(adresse);
await ctx.SaveChangesAsync();
adresseDto.Id = adresse.Id;
}
catch (Exception e)
{
Console.WriteLine("ohoh");
Console.WriteLine(e.ToString());
Console.WriteLine("ohoh");
return null;
}
return adresseDto;
}
UnitTest.cs
public async Task Test_AddTestAddress_ReturnsAddressWithId()
{
var dummyDataInDb = AddressHelper.GetAdressen(5);
var dummyData4Db = AddressHelper.GetAdressen(1).FirstOrDefault();
//AddressHelper returns just some dummy objects
InitializeMock<Adresse>.With(dummyDataInDb.AsQueryable(), out AdresseMock);
XaDbMock = new Mock<xaModel>();
XaDbMock
.Setup(x => x.Adresse)
.Returns(AdresseMock.Object);
_repository = NewAddressRepository(XaDbMock.Object);
//When
var addressesInDb = await _repository.GetAllAdressenAsync();
var result = await _repository.AddAdresseAsync(dummyData4Db);
var addressesInDbAfter = await _repository.GetAllAdressenAsync();
//Then
Assert.IsNotNull(addressesInDb);
Assert.AreEqual(5, addressesInDb.Count);
Assert.IsNotNull(result);
Assert.AreEqual(0, result.Id);
Assert.IsNotNull(addressesInDbAfter);
Assert.AreEqual(6, addressesInDbAfter.Count);
}
InitializeMock.cs
public class InitializeMock<T> where T : class
{
public static void With(IQueryable<T> dummyData, out Mock<DbSet<T>> mock)
{
mock = new Mock<DbSet<T>>();
//Required for async tasks
mock.As<IDbAsyncEnumerable>()
.Setup(x => x.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator<T>(dummyData.GetEnumerator()));
//Required for async tasks
mock.As<IQueryable<T>>()
.Setup(x => x.Provider)
.Returns(new TestDbAsyncQueryProvider<T>(dummyData.Provider));
mock.As<IQueryable<T>>()
.Setup(x => x.Expression)
.Returns(dummyData.Expression);
mock.As<IQueryable<T>>()
.Setup(x => x.ElementType)
.Returns(dummyData.ElementType);
mock.As<IQueryable<T>>()
.Setup(x => x.GetEnumerator())
.Returns(dummyData.GetEnumerator);
}
}
If something is missing, please let me know. I'll provide any neccessary informations you need.
Thanks in advance

Mocking EF with async and Include

I'm trying to mock Entity Framework. And my method which include Async and working with 2 tables of EF.
my method (MyClass.Create):
var my = new Application(title, "", creatorId, documentId, deadLine);
var document = await _db.Documents.FindAsync(my.DocumentId);
//some stuffs
//....
_db.My.Add(my);
await _db.SaveChangesAsync();
test:
private ApplicationDbContext context;
private DbSet<My> my;
private DbSet<Document> document;
private Document mDocument;
[SetUp]
public void Initialize()
{
// Instantiate mocks
context = MockRepository.GenerateMock<ApplicationDbContext>();
my = MockRepository.GenerateMock<DbSet<My>>();
document = MockRepository.GenerateMock<DbSet<Document>>();
mDocument = new Document(Guid.NewGuid().ToString(), "Про тест", "123456", Guid.NewGuid().ToString(), "12345", DateTime.Now, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
// Setup unit of work to return mocked repository
context.Stub(uow => uow.My).Return(my);
context.Stub(uow => uow.Documents).Return(document);
}
[Test]
public async Task Create_Consideration()
{
// Arrange
document.Stub(doc => doc.FindAsync(Arg<int>.Is.Anything)).Return(Task.FromResult(mDocument));
my.Expect(svc => svc.Add(Arg<My>.Is.Anything));
context.Expect(svc => svc.SaveChanges());
// Act
await MyClass.Create("Test", mDocument.CreatorId, mDocument.Id);
//Assert
my.VerifyAllExpectations();
context.VerifyAllExpectations();
}
}
Error what i get its: Method 'DbContext.SaveChangesAsync();' requires a return value or an exception to throw
Test must look like this:
my.Expect(svc => svc.Add(Arg<My>.Is.Anything));
context.Expect(svc => svc.SaveChangesAsync()).Return(Task.FromResult(Arg<int>.Is.GreaterThanOrEqual(0)));
// Act
await consideration.Create("Test", mDocument.CreatorId, mDocument.Id, null, new List<string> { cUser.Id, cUser2.Id });
//Assert
my.VerifyAllExpectations();
context.VerifyAllExpectations();

Unit test failing on EF Entry.State

Is it possible to unit test this?
public class MyRepository<T> where T : IdentityUser, new()
{
public async Task UpdateAsync(T user)
{
_context.Entry(user).State = EntityState.Modified;
_context.Entry(user).Property("UserName").IsModified = false;
await _context.SaveChangesAsync();
}
}
The [TestInitialize] adds 1 user to the repository
_user = new IdentityUser { Id = "70a038cdde40" };
IDbSet<IdentityUser> users = new FakeDbSet<IdentityUser> { _user };
var dbContext = new Mock<MyDbContext<IdentityUser>>();
dbContext.Setup(x => x.Users).Returns(() => users);
_repository = new MyRepository<IdentityUser>(dbContext.Object);
and I'm trying to test with this
private MyRepository<IdentityUser> _repository;
[TestMethod]
public async Task UpdateUser_Success2()
{
var user = await _repository.FindByIdAsync("70a038cdde40");
Assert.IsFalse(user.EmailConfirmed, "User.EmailConfirmed is True");
user.EmailConfirmed = true;
await _repository.UpdateAsync(user);
(...)
}
But it dies on 1st line of UpdateAsync. Is the test that is wrong or the UpdateAsync implementation? Is there any way I can test it?
Edit
I added as suggested by Belogix
dbContext.Setup(x => x.Entry(It.IsAny<IdentityUser>()))
.Returns(() => dbContext.Object.Entry(_user));
That gets me closer, I think, but still have the non-virtual error: Invalid setup on a non-virtual member: x => x.Entry(It.IsAny())
Best quote ever: "All problems in computer science can be solved by another level of indirection" - Butler Lampson.
It looks like this can't be tested directly without adding some additional abstraction. I had to refactor my UpdateAsync method this way
public async Task UpdateAsync(T user)
{
SetEntityStateModified(user);
SetPropertyIsModified(user);
await _context.SaveChangesAsync();
}
public virtual void SetPropertyIsModified(T user)
{
_context.Entry(user).Property("UserName").IsModified = false;
}
public virtual void SetEntityStateModified(T user)
{
_context.Entry(user).State = EntityState.Modified;
}
And then update my test code in the Initialize
_repository = new Mock<MyRepository<IdentityUser>>(dbContext.Object);
_repository.Setup(x => x.SetEntityStateModified(It.IsAny<IdentityUser>()));
_repository.Setup(x => x.SetPropertyIsModified(It.IsAny<IdentityUser>()));
My test then finally passes
[TestMethod]
public async Task can_update_user_details()
{
//Arrange
var user = await _repository.Object.FindByIdAsync("70a038cdde40");
Assert.IsFalse(user.EmailConfirmed, "User.EmailConfirmed is True");
//Act
user.EmailConfirmed = true;
await _repository.Object.UpdateAsync(user);
var newUser = await _repository.Object.FindByIdAsync("70a038cdde40");
//Assert
Assert.IsTrue(newUser.EmailConfirmed, "User.EmailConfirmed is False");
}
The ChangeTracker in dbContext tracks changes and hold the entities that are changed. So you can assert the changed entity is among them.
Assert.IsTrue(dbContext.Object.ChangeTracker.Entries().Any(entry =>
entry.State == EntityState.Modified &&
entry.Entity is IdentityUser &&
(entry.Entity as IdentityUser).Id == users[0].Id // Here you can check if it is actually the same user
));
For the property it would be something like this:
Assert.IsTrue(_context.Object.ChangeTracker.Entries().Any(entry =>
entry.Property("UserName").IsModified == false &&
entry.Entity is IdentityUser &&
(entry.Entity as IdentityUser).Id == users[0].Id // Here you can check if it is actually the same user
));
It looks like you have not stubbed your context correctly... I am not at a computer with Visual Studio so here is some pseudo code that should demonstrate what I mean. Replace the IsAnything with either your mocking frameworks way of ignoring argument or actually the user if you want to handle different responses.
// Existing context initialisation...
var dbContext = new Mock<MyDbContext<IdentityUser>>();
dbContext.Setup(x => x.Users).Returns(() => users);
// NEW: Mock what / how Entry is going to return when called (i.e. return a user)
dbContext.Setup(x => x.Entry(IsAnything)).Returns(() => users[0]);

Categories

Resources