编程语言
首页 > 编程语言> > c# – MoQ:属性已正确设置为虚拟但在非虚拟成员上仍设置无效

c# – MoQ:属性已正确设置为虚拟但在非虚拟成员上仍设置无效

作者:互联网

我想对继承自IdentityDbContext的DbContext进行单元测试

我收到一条带有非常常见消息的NotSupportedException错误消息:

System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member

为了找到一些线索,我已经阅读了很多帖子.以下是一些示例:

> This post
> This other post
> This Last one

显然,解决方案似乎将dbSet设置为virtual.我做了什么.

运气不好,错误仍然存​​在.

这是我的IdentityDataContext:

public class IdentityDataContext : IdentityDbContext<ApplicationUser>
{      
    public virtual DbSet<Search> Searches { get; set; }

    public IdentityDataContext()
        : base("LocalDb",
              throwIfV1Schema: true)
    {            
    }

    public static IdentityDataContext Create()
    {
        return new IdentityDataContext();
    }
}

这是我的搜索POCO:

public class Search : BaseEntity
{
    //BaseEntity is just an abstract class with createdAt,Id,updatedAt property
    public string ConsumerIdentifier { get; set; }
    public string LanguageCode { get; set; }         
}

最后这是我的设置方法:

[TestInitialize]
public void SetupTest()
{
    //DataInitializer.GetAllSearches() returns a List of "Search"

    _searches = DataInitializer.GetAllSearches();

    var dbSet = new Mock<DbSet<Search>>();
    dbSet.As<IQueryable<Search>>().Setup(m => m.Provider).Returns(_searches.AsQueryable().Provider);
    dbSet.As<IQueryable<Search>>().Setup(m => m.Expression).Returns(_searches.AsQueryable().Expression);
    dbSet.As<IQueryable<Search>>().Setup(m => m.ElementType).Returns(_searches.AsQueryable().ElementType);
    dbSet.As<IQueryable<Search>>().Setup(m => m.GetEnumerator()).Returns(_searches.AsQueryable().GetEnumerator());
    dbSet.Setup(d => d.Add(It.IsAny<Search>())).Callback<Search>(_searches.Add);

    _dataContextMock = new Mock<IdentityDataContext>()
    {
        CallBase = true
    };

    // The error appears here ---------------- _
    dataContextMock.Setup(x   => x.Searches).Returns(dbSet.Object);
    //----------------------------------------------------------------------

    _dataContextMock.Setup(x => x.Set<Search>()).Returns(dbSet.Object);

    _searchRepository = new SearchRepository(_dataContextMock.Object);
}

我错过了什么?

解决方法:

1)无需单元测试DbContext.微软已经对它进行过广泛的测试.

2)如果要将DbContext用作另一个类的依赖项,则抽象dbcontext

public interface IDataContext {
    DbSet<Search> Searches { get; set; }
}

public class IdentityDataContext : IdentityDbContext<ApplicationUser>, IDataContext { ... }

public class SearchRepository {
    public SearchRepository(IDataContext context) { ... }
}

根据结论而不是抽象,不要让你上课. IdentityDataContext是依赖类无需了解的实现细节.

这样就可以为测试提供更大的灵活性

Mock<IDataContext> _dataContextMock;

[TestInitialize]
public void SetupTest() {

    //DataInitializer.GetAllSearches() returns a List of "Search"
    _searches = DataInitializer.GetAllSearches();
    var queryable = _searches.AsQueryable();

    var dbSet = new Mock<DbSet<Search>>();
    dbSet.As<IQueryable<Search>>().Setup(m => m.Provider).Returns(queryable.Provider);
    dbSet.As<IQueryable<Search>>().Setup(m => m.Expression).Returns(queryable.Expression);
    dbSet.As<IQueryable<Search>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
    dbSet.As<IQueryable<Search>>().Setup(m => m.GetEnumerator()).Returns(() => queryable.GetEnumerator());
    dbSet.Setup(d => d.Add(It.IsAny<Search>())).Callback<Search>(_searches.Add);


    _dataContextMock = new Mock<IDataContext>();

    _dataContextMock.Setup(x => x.Searches).Returns(dbSet.Object);

    _dataContextMock.Setup(x => x.Set<Search>()).Returns(dbSet.Object);

    _searchRepository = new SearchRepository(_dataContextMock.Object);

}

标签:c,unit-testing,mstest,moq,entity-framework-6
来源: https://codeday.me/bug/20190711/1429279.html