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

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

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

问题

  1. 我有下面的接口,在应用程序中我正在使用Dapper并尝试使用NSubstitute模拟查询和连接。
  2. 在单元测试中
  3. [Fact]
  4. public void GetAllAuthority()
  5. {
  6. // 安排
  7. var connectionFactory = Substitute.For<IDapperContextBuilder>();
  8. var fakeConnection = Substitute.For<IDbConnection>();
  9. connectionFactory.CreateConnection().Returns(fakeConnection);
  10. // 模拟Dapper行为
  11. var authority = new MasterData.Authority();
  12. fakeConnection.QueryFirstOrDefaultAsync<MasterData.Authority>(Arg.Any<string>()).ReturnsForAnyArgs(authority);
  13. // 其余代码
  14. }
  15. 在控制器中
  16. public async Task UpdateStatus(AuthorityStatusUpdateInputDto input)
  17. {
  18. using var connection = _dapperContextBuilder.CreateConnection();
  19. Authority authority = await connection.QueryFirstOrDefaultAsync<Authority>("SELECT TOP 1 * FROM Authority");
  20. // 其余代码
  21. }
  22. 始终在行 `fakeConnection.QueryFirstOrDefaultAsync<MasterData.Authority>(Arg.Any<string>()).ReturnsForAnyArgs(authority);` 中收到异常:
  23. 无法为IDbCommand.set_CommandText返回类型为Task`1的值(期望类型为Void)。
  24. 确保在调用替代品后调用了Returns()(例如:mySub.SomeMethod().Returns(value)),
  25. 并且在Returns()内没有配置其他替代品(例如,避免这样做:mySub.SomeMethod().Returns(ConfigOtherSub()))。
  26. 如果您替代的是类而不是接口,请检查您对替代品的调用是否在虚拟/抽象成员上。
  27. 无法为非虚拟/非抽象成员配置返回值。
英文:

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

  1. public interface IDapperContextBuilder
  2. {
  3. IDbConnection CreateConnection();
  4. }

In the unit test

  1. [Fact]
  2. public void GetAllAuthority()
  3. {
  4. // Arrange
  5. var connectionFactory = Substitute.For&lt;IDapperContextBuilder&gt;();
  6. var fakeConnection = Substitute.For&lt;IDbConnection&gt;();
  7. connectionFactory.CreateConnection().Returns(fakeConnection);
  8. // Simulate Dapper behavior
  9. var authority = new MasterData.Authority();
  10. fakeConnection.QueryFirstOrDefaultAsync&lt;MasterData.Authority&gt;(Arg.Any&lt;string&gt;()).ReturnsForAnyArgs(authority);
  11. // rest of the code
  12. }

In controller

  1. public async Task UpdateStatus(AuthorityStatusUpdateInputDto input)
  2. {
  3. using var connection = _dapperContextBuilder.CreateConnection();
  4. Authority authority = await connection.QueryFirstOrDefaultAsync&lt;Authority&gt;(SELECT TOP 1 * FROM Authority&quot;);
  5. // rest of the code
  6. }

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

  1. Can not return value of type Task`1 for IDbCommand.set_CommandText (expected type Void).
  2. Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)),
  3. and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).
  4. If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member.
  5. Return values cannot be configured for non-virtual/non-abstract members.

答案1

得分: 1

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

我尝试使用这个包:

  1. var connectionFactory = Substitute.For<IDapperContextBuilder>();
  2. var fakeConnection = Substitute.For<IDbConnection>().SetupCommands();
  3. connectionFactory.CreateConnection().Returns(fakeConnection);
  4. var authority1 = new Authority() { Id = 1, Name = "N1" };
  5. var authority2 = new Authority() { Id = 2, Name = "N2" };
  6. 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:

  1. var connectionFactory = Substitute.For&lt;IDapperContextBuilder&gt;();
  2. var fakeConnection = Substitute.For&lt;IDbConnection&gt;().SetupCommands();
  3. connectionFactory.CreateConnection().Returns(fakeConnection);
  4. var authority1 = new Authority() { Id = 1, Name = &quot;N1&quot; };
  5. var authority2 = new Authority() { Id = 2, Name = &quot;N2&quot; } ;
  6. 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:

确定