如何MOQ实体框架类SqlQuery电话 [英] How to Moq Entity Framework SqlQuery calls

查看:179
本文介绍了如何MOQ实体框架类SqlQuery电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够模拟 DbSet 使用此的链接



不过,现在我想知道我怎么能嘲笑调用SqlQuery类。不知道这是可能的,或者怎么样,因为它依赖于嘲笑分贝范围内知道什么是查询被调用。



下面是我试图嘲弄。

  VAR myObjects = DbContext.Database 
.SqlQuery< MyObject的方式>(EXEC [DBO] [my_sproc] {0 },SOME_VALUE)
.ToList();



目前,我还没有尝试过的东西不知道如何开始嘲笑这个例子。



的嘲讽 DbSet 低于并再次重申,我可以正确模拟返回 DbSet 为MyObject 中的,但现在我试图嘲弄返回为MyObject

  VAR的DbContext =新的模拟< MyDbContext>(); 
dbContext.Setup(M = GT; m.MyObjects).Returns(mockObjects.Object);

dbContext.Setup(M = GT; m.Database.SqlQuery ......沿着这些线路
的东西


解决方案

数据库 财产的 类SqlQuery 方法未标记为虚拟让他们是轻慢不得;你可以使用不同的库能考虑到这一点,但比你可能更惯性。等等)



您可能需要使用某种抽象来解决这个问题,比如通过包装在一个辅助类数据库的整个查询:

 公共接口IQueryHelper 
{
&IList的LT;为MyObject> DoYourQuery(字符串值);
}

公共类QueryHelper:IQueryHelper
{
只读MyDbContext myDbContext;

公共QueryHelper(MyDbContext myDbContext)
{
this.myDbContext = myDbContext;
}

公众的IList<&MyObject的GT; DoYourQuery(字符串值)
{
返回myDbContext.Database.SqlQuery<&MyObject的GT(EXEC [DBO] [my_sproc] {0},值).ToList();
}
}

现在正在测试的方法变为:

 公共无效YourMethod()
{
VAR myObjects = queryHelper.DoYourQuery(SOME_VALUE);
}



然后你会注入 IQueryHelper 在您正在测试和模拟的类的构造函数。



您打算在 DoYourQuery失踪测试覆盖率,但现在查询这么简单看不出明显的不足之处


I've been able to mock DbSet's from entity framework with Moq using this link.

However, I would now like to know how I could mock the call to SqlQuery. Not sure if this is possible or how as it relies on the mocked db context knowing what "query" is being called.

Below is what I am trying to mock.

var myObjects = DbContext.Database
    .SqlQuery<MyObject>("exec [dbo].[my_sproc] {0}", "some_value")
    .ToList();

I currently haven't tried anything as did not know how to start mocking this example.

The mocking of the DbSet is below and to re-iterate, I can correctly mock returning a DbSet of MyObject's but now am trying to mock a SqlQuery that returns a list of MyObject's.

var dbContext = new Mock<MyDbContext>();
dbContext.Setup(m => m.MyObjects).Returns(mockObjects.Object);

dbContext.Setup(m => m.Database.SqlQuery... something along these lines

解决方案

The Database property and SqlQuery method are not marked as virtual so they can't be mocked (using Moq; you could use a different library that can account for this but that may be more inertia than you'd like).

You'd need to use some sort of abstraction to get around this, such as by wrapping the entire query of the database in a helper class:

public interface IQueryHelper
{
    IList<MyObject> DoYourQuery(string value);
}

public class QueryHelper : IQueryHelper
{
    readonly MyDbContext myDbContext;

    public QueryHelper(MyDbContext myDbContext)
    {
        this.myDbContext = myDbContext;
    }

    public IList<MyObject> DoYourQuery(string value)
    {
        return myDbContext.Database.SqlQuery<MyObject>("exec [dbo].[my_sproc] {0}", value).ToList();
    }
}

Now the method you are testing becomes:

public void YourMethod()
{
    var myObjects = queryHelper.DoYourQuery("some_value");
}

Then you'd inject the IQueryHelper in the constructor of the class you're testing and mock that.

You're going to be missing test coverage on DoYourQuery, but the now the query is so simple there are obviously no deficiencies.

这篇关于如何MOQ实体框架类SqlQuery电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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