莫青EF抛出异常

时间:2015-08-05 08:17:58

标签: c# entity-framework unit-testing

我正在尝试编写单元测试,但遇到了一个奇怪的问题

 [TestMethod]
    public void Delete_user_save_via_context()
    {

        var data = new List<admins> 
        { 
            new admins() {id = 1,  login = "test" },

        }.AsQueryable();


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

        var mockContext = new Mock<Entities>();
        mockContext.Setup(x => x.admins).Returns(mockSet.Object);  //Here i have Exception


        var userService = new UserService(mockContext.Object);
        userService.Delete("test");

        mockSet.Verify(m => m.Remove(It.IsAny<admins>()),Times.Once);
        mockContext.Verify(m => m.SaveChanges(), Times.Once);

    }

抛出异常:

  

Castle.DynamicProxy.InvalidProxyConstructorArgumentsException:不能   实例化类的代理:找不到会有的构造函数   匹配给定的参数:System.Linq.EnumerableQuery`

有人可以帮忙吗?

管理类的实施:

public partial class admins
{
    public int id { get; set; }
    public string login { get; set; }   
}

1 个答案:

答案 0 :(得分:0)

var mockSet = new Mock<DbSet<admins>>(data);

应该是:

var mockSet = new Mock<DbSet<admins>>();