System.NotSupportedException:在非虚拟(在VB中可重写)成员上的无效设置 [英] System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member
问题描述
我使用Moq在单元测试中收到一条NotSupportedException错误消息
I am getting a NotSupportedException error message on my Unit Test using Moq
System.NotSupportedException:在非虚拟(在VB中可重写)成员上的无效设置
System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member
单元测试代码:
[TestMethod]
public void TestEmailNotSentOut()
{
// ...
var dataAccess = new Mock<TjiContext>();
var mockSetStock = new Mock<DbSet<Stock>>();
mockSetStock.As<IQueryable<Stock>>().Setup(m => m.Provider).Returns(stockList.Provider);
mockSetStock.As<IQueryable<Stock>>().Setup(m => m.Expression).Returns(stockList.Expression);
mockSetStock.As<IQueryable<Stock>>().Setup(m => m.ElementType).Returns(stockList.ElementType);
mockSetStock.As<IQueryable<Stock>>().Setup(m => m.GetEnumerator()).Returns(stockList.GetEnumerator());
dataAccess.Setup(m => m.Stocks).Returns(mockSetStock.Object);
此帖子中的建议说将其标记为virtual
,但我不确定需要将哪些标记为虚拟?
A suggestion in this post says to mark it as virtual
, but I'm not sure what needs to be marked as virtual?
此行发生错误:
dataAccess.Setup(m => m.Stocks).Returns(mockSetStock.Object);
推荐答案
Assuming you're using EF of at least V6 and based on this example (look at the Blogs element) which is doing a very similar thing to you. I'd guess that your problem is that your dataAccess
, whatever it is doesn't declare Stocks
as virtual.
所以它应该看起来像这样:
So it should look something like this:
public virtual DbSet<Stock> Stocks { get; set; }
这篇关于System.NotSupportedException:在非虚拟(在VB中可重写)成员上的无效设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!