51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException: Can not return value of type Task`1 for IDbCommand. (expected type Void)

英文:

NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException: Can not return value of type Task`1 for IDbCommand. (expected type Void)

问题 {#heading}

我有下面的接口,在应用程序中我正在使用Dapper并尝试使用NSubstitute模拟查询和连接。

在单元测试中

[Fact]
public void GetAllAuthority()
{
    // 安排
    var connectionFactory = Substitute.For<IDapperContextBuilder>();
    var fakeConnection = Substitute.For<IDbConnection>();
    connectionFactory.CreateConnection().Returns(fakeConnection);
// 模拟Dapper行为
var authority = new MasterData.Authority();
fakeConnection.QueryFirstOrDefaultAsync<MasterData.Authority>(Arg.Any<string>()).ReturnsForAnyArgs(authority);

// 其余代码

}

在控制器中

public async Task UpdateStatus(AuthorityStatusUpdateInputDto input)
{
using var connection = _dapperContextBuilder.CreateConnection();
Authority authority = await connection.QueryFirstOrDefaultAsync<Authority>("SELECT TOP 1 * FROM Authority");

// 其余代码

}

始终在行 fakeConnection.QueryFirstOrDefaultAsync<MasterData.Authority>(Arg.Any<string>()).ReturnsForAnyArgs(authority); 中收到异常:

无法为IDbCommand.set_CommandText返回类型为Task`1的值(期望类型为Void)。

确保在调用替代品后调用了Returns()(例如:mySub.SomeMethod().Returns(value)), 并且在Returns()内没有配置其他替代品(例如,避免这样做:mySub.SomeMethod().Returns(ConfigOtherSub()))。

如果您替代的是类而不是接口,请检查您对替代品的调用是否在虚拟/抽象成员上。 无法为非虚拟/非抽象成员配置返回值。

英文:

I have the below interface, in the application I am using Dapper and I am trying to mock the query and connection using NSubstitude.

public interface IDapperContextBuilder
{
    IDbConnection  CreateConnection();
}

In the unit test

[Fact]
        public void GetAllAuthority()
        {
            // Arrange
            var connectionFactory = Substitute.For<IDapperContextBuilder>();
            var fakeConnection = Substitute.For<IDbConnection>();
            connectionFactory.CreateConnection().Returns(fakeConnection);
        // Simulate Dapper behavior
        var authority = new MasterData.Authority();
        fakeConnection.QueryFirstOrDefaultAsync<MasterData.Authority>(Arg.Any<string>()).ReturnsForAnyArgs(authority);
    // rest of the code
}


In controller

public async Task UpdateStatus(AuthorityStatusUpdateInputDto input)
{
using var connection = _dapperContextBuilder.CreateConnection();
Authority authority = await connection.QueryFirstOrDefaultAsync<Authority>(SELECT TOP 1 * FROM Authority");

// rest of the code

}


Keep getting the exception in line fakeConnection.QueryFirstOrDefaultAsync<MasterData.Authority>(Arg.Any<string>()).ReturnsForAnyArgs(authority); as

Can not return value of type Task`1 for IDbCommand.set_CommandText (expected type Void).

Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)), and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).

If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member. Return values cannot be configured for non-virtual/non-abstract members.


答案1 {#1}

得分: 1

这里是一个名为 NSubstitute.Community.DbConnection

我尝试使用这个包:

var connectionFactory = Substitute.For<IDapperContextBuilder>();
var fakeConnection = Substitute.For<IDbConnection>().SetupCommands();

connectionFactory.CreateConnection().Returns(fakeConnection);

var authority1 = new Authority() { Id = 1, Name = "N1" }; var authority2 = new Authority() { Id = 2, Name = "N2" };

fakeConnection.SetupQuery("SELECT * FROM Authority").Returns(authority1, authority2);

当我调试测试时:

NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException: Can not return value of type Task`1 for IDbCommand. (expected type Void)

NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException: Can not return value of type Task`1 for IDbCommand. (expected type Void)

您可以根据这个包中的代码进行进一步的需求分析。 英文:

Here's a package NSubstitute.Community.DbConnection

I tried with the package:

var connectionFactory = Substitute.For&lt;IDapperContextBuilder&gt;();
            var fakeConnection = Substitute.For&lt;IDbConnection&gt;().SetupCommands();
        connectionFactory.CreateConnection().Returns(fakeConnection);
    var authority1 = new Authority() { Id = 1, Name = &amp;amp;amp;quot;N1&amp;amp;amp;quot; };
    var authority2 = new Authority() { Id = 2, Name = &amp;amp;amp;quot;N2&amp;amp;amp;quot; }  ;
fakeConnection.SetupQuery(&amp;amp;amp;amp;quot;SELECT * FROM Authority&amp;amp;amp;amp;quot;).Returns(authority1, authority2);


When I debug the test:

NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException: Can not return value of type Task`1 for IDbCommand. (expected type Void)

NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException: Can not return value of type Task`1 for IDbCommand. (expected type Void)

you could follow the codes in this package for further requirements


赞(6)
未经允许不得转载:工具盒子 » NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException: Can not return value of type Task`1 for IDbCommand. (expected type Void)