英文:
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<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
这里是一个名为 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);
当我调试测试时:
您可以根据这个包中的代码进行进一步的需求分析。
英文:
Here's a package NSubstitute.Community.DbConnection
I tried with the package:
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);
When I debug the test:
you could follow the codes in this package for further requirements
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论