c# - Moq.Mock<T> 使用 MOQ 将表达式设置为 Mock 会导致模拟设置不匹配

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

我正在尝试模拟一个数据服务上下文,作为其中的一部分,我有一个接受

的方法
  • 一个表达式(谓词),
  • 一个可选的字符串参数
  • 带有谓词数组的参数。

当我尝试模拟这个方法时,MOQ 总是返回一个

模拟上的所有调用都必须具有相应的设置。 TearDown:Moq.MockException:不匹配以下设置: IContext m => m.Retrieve(It.IsAny() })

接口(interface)/实现下面的代码

public interface IContext
{
    IQueryable<T> Retrieve<T>(Expression<Func<T, bool>> predicate,
                                string entitySetName = null,
                                params Expression<Func<T, object>>[] eagerProperties);
}

public class Context : IContext
{
    private readonly DataServiceContext _context;

    public Context(DataServiceContext context)
    {
        this._context = context;
    }

    public IQueryable<T> Retrieve<T>(Expression<Func<T, bool>> predicate,
                                        string entitySetName = null,
                                        params Expression<Func<T, object>>[] eagerProperties)
    {
        DataServiceQuery<T> query = _context.CreateQuery<T>(entitySetName ?? "Default");
        return eagerProperties.Aggregate(query, (current, e) => current.Expand(e.ToString())).Where(predicate);
    }
}

下面是调用上面上下文方法的测试类

public class Test
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
}

public class SomeController
{
    private IContext _context = new Context(
        new DataServiceContext(new Uri("http://whatever/someservice.svc/")));

    public IContext ServiceContext
    {
        get
        {
            return _context ??
                   (_context = new Context(new DataServiceContext(new Uri("http://whatever/someservice.svc/"))));
        }
        set
        {
            _context = value;
        }
    }

    public Test RetrieveSomeInformation()
    {
        IQueryable<Test> tests = _context.Retrieve<Test>
                                                (
                                                    //Param 1
                                                    t => t.Prop1 == "test" && 1 == 1,

                                                    //Param 2
                                                    "Test",

                                                    //Param 3
                                                    t => t.Prop1,
                                                    t => t.Prop2,
                                                    t => t.Prop3
                                                  );
        return tests.First();
    }
}

下面是 MOQ 失败的实际测试,“模拟上的所有调用都必须有相应的设置。”看不出为什么设置不匹配!任何帮助将不胜感激。

[TestFixture]
public class ControllerTests
{
    public MockRepository Repository { get; set; }
    protected Mock<IContext> MockContext { get; set; }
    public SomeController Controller;

    public List<Test> Tests;
    public Test Test;

    [SetUp]
    public void SetUp()
    {
        Test = new Test { Prop1 = "1", Prop2 = "2", Prop3 = "3" };
        Tests = new List<Test> { Test };

        Repository = new MockRepository(MockBehavior.Strict);

        MockContext = Repository.Create<IContext>();

        Controller = new SomeController { ServiceContext = MockContext.Object };
    }

    [TearDown]
    public void TearDown()
    {
        Repository.VerifyAll();
    }

    [Test]
    public void DetailProgramme_Test()
    {
        MockContext.Setup(m => m.Retrieve<Test>
                            (
                                //Param 1
                                It.IsAny<Expression<Func<Test, bool>>>(),

                                //Param 2
                                It.IsAny<string>(),

                                //Param 3
                                It.IsAny<Expression<Func<Test, object>>>()
                            )
                          ).Returns(Tests.AsQueryable());

        Test info = Controller.RetrieveSomeInformation();


        //myMock.Setup(r => r.Find(It.IsAny<Expression<Func<Person, bool>>>())).Returns(new List<Person>() { new Person() }.AsQueryable());
        Assert.IsTrue(info == Test);
    }
}

最佳答案

我相信这取决于您的设置...

//Param 3
It.IsAny<Expression<Func<Test, object>>>()

与参数数组不匹配。尝试...

//Param 3
It.IsAny<Expression<Func<Test, object>>[]>()

关于c# - Moq.Mock<T> 使用 MOQ 将表达式设置为 Mock 会导致模拟设置不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8514448/

相关文章:

c# - 如何将 asp.net "Login" View 放在布局页面上,而不是 @Html.Actionlink 到单独的登录页面?

c# - Windows UWP Pivot 轻扫 UI

entity-framework - 代码优先 EF6 SqlServerMigrationSqlGenerator 中的自定义逻辑不起作用

c# - 带有私有(private)字段的 INotifyPropertyChanged

c# - 我有一个包含 RTP 数据包 : now what? 的日志文件

scala - 如何默认禁用 "Slow"标记的 Scalatests,允许使用选项执行?

c# - 多文件删除方法单元测试成功

javascript - 如何 Jest 从覆盖报告生成中删除快照的考虑

c# - AutoMapper.Collection.EntityFramework 在 Persist.InsertOrUpdate 之后导致 InvalidOperationException

entity-framework - 防止 EF 尝试更新/插入子对象