Make it easier to mock DbSet.Add

See original GitHub issue

##upgrading from EF Core 5.0.2 to 6.0.1 making Nunit unit test to fail

Currently I have upgraded from .net core 5 to .net 6, so I also have to update ef core from 5 to 6.0.1. However doing this is failing some of my unit test written in nunit and using Moq. Issue is occuring while mocking ‘InternalEntityEntry’ class which is sealed in 6.0.1 but abstract in 5.0.2.

Below is the code that I am using to create Moq:

var entry = new Mock<InternalEntityEntry>(new Mock<IStateManager>().Object,
                new EntityType(typeof(T), new Model(), true, ConfigurationSource.Explicit)).Object;
            var entityEntry = new Mock<EntityEntry<T>>(entry);
            entityEntry.SetupGet(self => self.Entity).Returns(data);
            mockSet.Setup(d => d.AddAsync(It.IsAny<T>(), default)).Returns(() => new ValueTask<EntityEntry<T>>(entityEntry.Object));

and below is the error received:

System.NotSupportedException : Type to mock (InternalEntityEntry) must be an interface, a delegate, or a non-sealed, non-static class.

Help me in resolving this.

Include provider and version information

EF Core version: 6.0.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: IDE: Visual Studio 2022

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

9reactions
ajcvickerscommented, Jan 10, 2022

@roji It is not required that InternalEntityEnrtry be mocked for this. For example:

var internalEntityEntry = new InternalEntityEntry(
    new Mock<IStateManager>().Object,
    new RuntimeEntityType("T", typeof(T), false, null, null, null, ChangeTrackingStrategy.Snapshot, null, false),
    data);

var entityEntry = new Mock<EntityEntry<T>>(internalEntityEntry);

That being said, EntityEntry<T> is not easy to mock. At the time we considered it something that people shouldn’t do, but I don’t think that took into account that methods like Add would return one. (In EF6, we returned just the instance, not the entry.)

2reactions
rojicommented, Jan 5, 2022

@RitikaPoddar mocking an internal EF type is not something that should be done in application testing - by definition these types don’t represent the public API of the library, may change at any moment and should not be needed.

Can you provide more context on exactly what you’re trying to test, and provide the code for the entire test? We may be able to provide a better approach to doing that testing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add an item to a Mock DbSet (using Moq)
The problem is that when you Add something to the database you'll have to manually set the navigation properties, while this mocking will...
Read more >
DbContext / DbSet Mock for Unit Test in C# with Moq
With this, I have my DbSet ready to be called as a List and be used to retrieve data with Linq or even...
Read more >
Testing with a mocking framework - EF6
The easiest way to get Moq is to install the Moq package from NuGet. ... this article is dependent on some changes we...
Read more >
Mocking DbContext and DbSet with Moq - Michał Jankowski
Specifically this line that performs an invalid cast: usersMock.Setup(x => x.Add(It.IsAny())).Returns((User u) => u); The mock set up using ...
Read more >
How to Mock EF Core DbContext
In this article we'll talk about how to mock EF Core DbContext using different libraries and when to use which one.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found