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

huangapple go评论107阅读模式
英文:

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

问题

我有下面的接口,在应用程序中我正在使用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&lt;IDapperContextBuilder&gt;();
            var fakeConnection = Substitute.For&lt;IDbConnection&gt;();
            connectionFactory.CreateConnection().Returns(fakeConnection);


            // Simulate Dapper behavior
            var authority = new MasterData.Authority();
            fakeConnection.QueryFirstOrDefaultAsync&lt;MasterData.Authority&gt;(Arg.Any&lt;string&gt;()).ReturnsForAnyArgs(authority);

            // rest of the code
        }

In controller

public async Task UpdateStatus(AuthorityStatusUpdateInputDto input)
{
    
    using var connection = _dapperContextBuilder.CreateConnection();
    Authority authority = await connection.QueryFirstOrDefaultAsync&lt;Authority&gt;(SELECT TOP 1 * FROM Authority&quot;);

    // rest of the code
    
}

Keep getting the exception in line fakeConnection.QueryFirstOrDefaultAsync&lt;MasterData.Authority&gt;(Arg.Any&lt;string&gt;()).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

这里是一个名为 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 = &quot;N1&quot; };
            var authority2 = new Authority() { Id = 2, Name = &quot;N2&quot; }  ;
            
            fakeConnection.SetupQuery(&quot;SELECT * FROM Authority&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

huangapple
  • 本文由 发表于 2023年8月10日 09:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定