为什么我的模拟对象没有返回任何结果?

时间:2015-07-16 07:49:01

标签: c# entity-framework unit-testing moq

我正在尝试创建DBContextDbSet的模拟。我认为我正确设置DBContext,反过来设置DbSet,但即使我向DBSet添加元素,它仍然会返回null。

我做错了什么?

首先,我按如下方式进行设置:

[SetUp]
public void Setup_Tests()
{
    Database.SetInitializer(new DropCreateDatabaseAlways<SubscriptionManagementContext>());

    var mock = new Mock<SubscriptionManagementContext>();
    mock.Setup(xx => xx.UIElements).Returns(GetMockDBSet(SubManInitializer.GetUIElements));

    _subscriptionManagementContext = mock.Object;
}

SubscriptionManagementContext定义为:

public class SubscriptionManagementContext : DbContext
{
    public SubscriptionManagementContext()
        : base("SubscriptionManagementContext")
    {
    }

    public virtual DbSet<UIElement> UIElements { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}   

此处setupAction.Invoke()应返回List<UIElement>

中定义的GetUIElements()
private static DbSet<T> GetMockDBSet<T>(Func<List<T>> setupAction) where T : class
{
    var mockDBSet = new Mock<DbSet<T>>();

    mockDBSet.Setup(xx => xx.AddRange(setupAction.Invoke()));

    return mockDBSet.Object;
}   

public static List<UIElement> GetUIElements()
{
    var uiElements = new List<UIElement>
    {
        new UIElement {ElementName = "EmailDetails" },
        new UIElement {ElementName = "SFTPDetails"      },
        new UIElement {ElementName = "ScheduleDetails"  },
        new UIElement {ElementName = "FileNameElement"  },
    };
    return uiElements;
}

当我调试此测试时,我可以看到GetUIElements()已执行,因此我认为uiElements对象可能包含数据,而是抛出ArgumentNullException

[Test]
public void Can_Get_UIElements()
{
    var uiElements = _subscriptionManagementContext.UIElements;

    Assert.IsNotNull(uiElements);

    Assert.IsTrue(uiElements.Any()); // throws System.ArgumentNullException 
}

我需要做些什么才能确保uiElements包含数据?

修改

根据要求,堆栈跟踪:

at System.Linq.Expressions.Expression.RequiresCanRead(Expression expression, String paramName)
at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression[] arguments)
at System.Linq.Queryable.Any[TSource](IQueryable`1 source)
at tstReportSubscriptionManagement.Test.SubManTests.Can_Get_UIElements() in c:\git\tst\tstReportSubscriptionMgmt\tstReportSubscriptionManagement.Test\SubManTests.cs:line 74

1 个答案:

答案 0 :(得分:1)

由于您的模拟DbSet旨在处理IQueryable<T>数据,AddRange不足以使其像常规List<T>一样工作。

您需要模拟IQueryable<T>的遗漏实现:

private static DbSet<T> GetMockDBSet<T>(Func<List<T>> setupAction) where T : class
{
    var mockDBSet = new Mock<DbSet<T>>();
    var mockedData = setupAction.Invoke().AsQueryable();

    mockDBSet.As<IQueryable<T>>().Setup(x => x.Provider).Returns(mockedData.Provider);
    mockDBSet.As<IQueryable<T>>().Setup(x => x.Expression).Returns(mockedData.Expression);
    mockDBSet.As<IQueryable<T>>().Setup(x => x.ElementType).Returns(mockedData.ElementType);
    mockDBSet.As<IQueryable<T>>().Setup(x => x.GetEnumerator()).Returns(mockedData.GetEnumerator());

    return mockDBSet.Object;
}