Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DbSet mocking is broken #1091

Closed
Tracked by #1093
rleeson24 opened this issue Oct 30, 2020 · 2 comments
Closed
Tracked by #1093

DbSet mocking is broken #1091

rleeson24 opened this issue Oct 30, 2020 · 2 comments

Comments

@rleeson24
Copy link

I just upgraded my Moq Nuget reference from 4.13.0 to 4.14.7 and my tests started failing. I tracked it down to this piece of code that sets up mocks for the DbSet. I could not determine what Moq is expecting. I did not play around with other versions between these two to know which one it started breaking on.

public static Mock<DbSet<T>> CreateDbSetMock<T>( IEnumerable<T> elements ) where T : class
		{
			var elementsAsQueryable = elements.AsQueryable();
			var dbSetMock = new Mock<DbSet<T>>();

			dbSetMock.As<IQueryable<T>>().Setup( m => m.Provider ).Returns( elementsAsQueryable.Provider );
			dbSetMock.As<IQueryable<T>>().Setup( m => m.Expression ).Returns( elementsAsQueryable.Expression );
			dbSetMock.As<IQueryable<T>>().Setup( m => m.ElementType ).Returns( elementsAsQueryable.ElementType );
			dbSetMock.As<IQueryable<T>>().Setup( m => m.GetEnumerator() ).Returns( elementsAsQueryable.GetEnumerator() );
			//dbSetMock.Setup( m => m.Include( It.IsAny<Expression<Func<T, IQueryable<T>>>>() ) ).Returns( dbSetMock.Object );
			dbSetMock.Setup( m => m.Include( It.IsAny<String>() ) ).Returns( dbSetMock.Object );

			return dbSetMock;
		}

TearDown : Moq.MockException : The mock repository failed verification due to the following:

   Mock<IGuildDbContext:14>:
   This mock failed verification due to the following:
   
      IGuildDbContext m => m.RankRequirements:
      
         Mock<DbSet<RankRequirement>:3>:
         This mock failed verification due to the following:
         
            IQueryable<RankRequirement> m => m.ElementType:
            This setup was not matched.
         
            IQueryable<RankRequirement> m => m.GetEnumerator():
            This setup was not matched.
         
            DbSet<RankRequirement> m => m.Include(It.IsAny<string>()):
            This setup was not matched.
   
      IGuildDbContext m => m.RankStatusRings:
      
         Mock<DbSet<RankStatusCompletedRing>:3>:
         This mock failed verification due to the following:
         
            IQueryable<RankStatusCompletedRing> m => m.ElementType:
            This setup was not matched.
         
            IQueryable<RankStatusCompletedRing> m => m.GetEnumerator():
            This setup was not matched.
         
            DbSet<RankStatusCompletedRing> m => m.Include(It.IsAny<string>()):
            This setup was not matched.
   
      IGuildDbContext m => m.RankStatusCrosses:
      
         Mock<DbSet<RankStatusCompletedCross>:3>:
         This mock failed verification due to the following:
         
            IQueryable<RankStatusCompletedCross> m => m.ElementType:
            This setup was not matched.
         
            IQueryable<RankStatusCompletedCross> m => m.GetEnumerator():
            This setup was not matched.
         
            DbSet<RankStatusCompletedCross> m => m.Include(It.IsAny<string>()):
            This setup was not matched.
   
      IGuildDbContext m => m.RingApprovals:
      
         Mock<DbSet<RingApproval>:3>:
         This mock failed verification due to the following:
         
            IQueryable<RingApproval> m => m.ElementType:
            This setup was not matched.
         
            IQueryable<RingApproval> m => m.GetEnumerator():
            This setup was not matched.
         
            DbSet<RingApproval> m => m.Include(It.IsAny<string>()):
            This setup was not matched.
   
      IGuildDbContext m => m.CrossStatusEntries:
      
         Mock<DbSet<CrossStatusEntry>:3>:
         This mock failed verification due to the following:
         
            IQueryable<CrossStatusEntry> m => m.ElementType:
            This setup was not matched.
         
            IQueryable<CrossStatusEntry> m => m.GetEnumerator():
            This setup was not matched.
         
            DbSet<CrossStatusEntry> m => m.Include(It.IsAny<string>()):
            This setup was not matched.
@stakx
Copy link
Contributor

stakx commented Nov 1, 2020

Hard to say for sure without being able to test with an actual repro code. The most likely culprit is, once again, recursive verification. As a workaround, try replacing all .Returns(...) in your helper method with .Returns(() => ...).

@stakx
Copy link
Contributor

stakx commented Dec 29, 2020

Closing due to not getting any response. Not much to be done without repro code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants