System.NotSupportedException:在非虚拟(在VB中可重写)成员上的无效设置 [英] System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member

查看:73
本文介绍了System.NotSupportedException:在非虚拟(在VB中可重写)成员上的无效设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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);

推荐答案

假设您使用的是至少为V6的EF,并且基于此

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆