Error when calling Mock<>.Setup for mocked DbContext

357 views
Skip to first unread message

William Bain

unread,
Nov 3, 2014, 7:08:08 PM11/3/14
to moq...@googlegroups.com
Here is the problematic code:

           
            var mockSet = new Mock<DbSet<Posting>>();
            mockSet
.As<IQueryable<Posting>>().Setup(m => m.Provider).Returns(data.Provider);
            mockSet
.As<IQueryable<Posting>>().Setup(m => m.Expression).Returns(data.Expression);
            mockSet
.As<IQueryable<Posting>>().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet
.As<IQueryable<Posting>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

           
var mockContext = new Mock<CustomDbContext>();
            mockContext
.Setup(m => m.Postings).Returns(mockSet.Object);



It fails with a Castle proxy error:

Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

I am following the steps to the letter Microsoft's guide to mocking in EF 6here:

http://msdn.microsoft.com/en-us/data/dn314429

The custom DbContext is set up correctly with the required DbSet<> properties marked as virtual and with public get/set.

Any help is appreciated.


Will.

Chris F Carroll

unread,
Mar 11, 2015, 2:42:00 PM3/11/15
to moq...@googlegroups.com
Updating to latest v4.2.xxx of Moq fixed this for me.
Reply all
Reply to author
Forward
0 new messages