![](/img/trans.png)
[英]Mocking a DbConnection with NUnit/NSubstitute/AutoFixture and InsightDatabase
[英]NSubstitute with DbConnection
我需要將 DbConnection 傳遞給我的 class。 為此,我正在使用 NSubstitute。 但是當我運行測試時,會出現以下錯誤:“System.NullReferenceException:Object 引用未設置為 object 的實例。Dapper”
我的測試
public class SceneApplicationServiceTest
{
private readonly DbConnection _connection = Substitute.For<DbConnection>();
ISceneApplicationService _sceneApplicationService;
public SceneApplicationServiceTest()
{
_sceneApplicationService = new SceneApplicationService(_connection);
}
[Fact]
public async Task ShouldBePossibleSuccessfullyGetByProgramScript()
{
const long programId = 1;
const string scriptId = "2";
await _sceneApplicationService.GetByProgramScript(programId, scriptId);
await _sceneApplicationService.Received(1).GetByProgramScript(programId, scriptId);
}
}
我的方法
public class SceneApplicationService : ISceneApplicationService
{
private readonly DbConnection _connection;
public SceneApplicationService(DbConnection connection)
{
_connection = connection;
}
public async Task<IEnumerable<LegacySceneResponse>> GetByProgramScript(long programId, string scriptId)
{
object parameters = new
{
CodigoPrograma = programId,
CodigoRoteiro = scriptId
};
if (!ObjectValidation.IsInvalidAnyNullOrEmpty(parameters)) ;
return await _connection.QueryAsync<LegacySceneResponse>(GetScenesByScriptAndProgram.Query, parameters);
}}
當測試嘗試運行“QueryAsync”時發生錯誤
有任何想法嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.