使用 Moq 时在 Dapper 方法上出现 NotSupportedException

问题描述 投票:0回答:2

使用

Moq
时,我收到以下异常:

System.NotSupportedException: 'Expression references a method that does not belong to the mocked object: c => c.Query<MyClass>(It.IsAny<String>(), It.IsAny<Object>(), It.IsAny<IDbTransaction>(), It.IsAny<Boolean>(), It.IsAny<Nullable`1>(), (Nullable`1)It.IsAny<CommandType>())'

我的班级:

public class MyClass
{
    public int Id {get; set;}
    public string Name {get; set;}
}

我的实际 BI 课程。我在这门课上使用

Dapper

using Dapper;

//**
//**
//**
using (var con = _readRepository.CreateConnection())
{
    var query = "Select * FROM myTable"
    return con.Query<MyClass>(query, new { Skip = 0, Take = 10}, null, true, null, null);
}

我的单元测试:

var conMock = new Mock<IDbConnection>();

IEnumerable<MyClass> listModels = new List<MyClass>().AsEnumerable();

//The exception occurrs right here
conMock.Setup(c => c.Query<MyClass>(
        It.IsAny<string>(),
        It.IsAny<object>(),
        It.IsAny<IDbTransaction>(),
        It.IsAny<bool>(),
        It.IsAny<int?>(),
        It.IsAny<CommandType>()
))
.Returns(() => listModels);

//System.NotSupportedException: 'Expression references a method that does not belong to the mocked object: c => c.Query<MyClass>(It.IsAny<String>(), It.IsAny<Object>(), It.IsAny<IDbTransaction>(), It.IsAny<Boolean>(), It.IsAny<Nullable`1>(), (Nullable`1)It.IsAny<CommandType>())'

我只想模仿

Query<MyClass>
方法。 我做错了什么?

c# unit-testing moq dapper notsupportedexception
2个回答
5
投票

Query<T>
是一种扩展方法。

public static IEnumerable<T> Query<T>(
    this IDbConnection cnn, 
    string sql, 
    object param = null, 
    SqlTransaction transaction = null, 
    bool buffered = true
)

Moq 但是不能模拟扩展方法。因此,要么模拟该扩展方法内部完成的操作,这将涉及必须检查 Dapper 源代码

将该功能封装在您控制并可以模拟的抽象背后。


2
投票

我倾向于用我自己的对象包装外部库,以使测试变得容易并且语言更容易品味。此外,您还可以将这些库中的潜在更改与包装对象隔离。此外,您还可以快速向方法添加缓存等功能。但最重要的是,因为它与这个问题相关,你可以轻松地嘲笑它。

public interface IDatabase{

IDbConnection GetConnection();
IEnumerable<T> Query<T>(/* whatever you want here...exactly Dapper's parameters if necessary */);

}

public class Database : IDatabase{
     //implement GetConnection() however you like...open it too!
     public IEnumerable<T> Query<T>(/*...parameters...*/){

     IEnumerable<T> query = null;
     using(conn = this.GetConnection()){
          query = conn.Query<T>()//dapper's implementation
     }
     return query;
   }
}

现在您可以完全控制模拟您的 IDatabase。

var mockDb = new Mock<IDatabase>();
mockDb.Setup(s=>s.Query(It.IsAny<>/*...whatever params...*/).Returns(/*...whatever you want to return...*/)
© www.soinside.com 2019 - 2024. All rights reserved.