英文:
Due to AutoFixture and NSubstitude object reference not set to an instance of an object error from unit test
问题
错误似乎出现在主要代码的这一行:
var parameter = await repository.GetFirstOrDefaultAsync(predicate: q => q.Id == parameterDto.Id);
这里似乎出现了一个对象引用错误。可能是因为 parameterDto.Id 为 null 或者没有找到与该 Id 匹配的记录。
请检查以下几点:
- 确保 parameterDto.Id 不为 null。
- 确保在 repository 中有与 parameterDto.Id 匹配的记录,否则会返回 null。
你可以通过在调试模式下逐步执行代码来进一步诊断和解决这个问题。
英文:
I am writing unit test in mvc project with AutoFixture and NSubstitude. But I am getting object reference error in my update and delete methods. Probably because I'm new, I may be missing the settings for these packages.
This is my main code
public async Task<ServiceResponse<ParameterDto>> Update(ParameterDto parameterDto)
{
try
{
if (parameterDto == null)
{
return serviceResponseHelper.SetError<ParameterDto>(null, localize["InvalidModel"], StatusCodes.Status400BadRequest, true);
}
var parameter = await repository.GetFirstOrDefaultAsync(predicate: q => q.Id == parameterDto.Id);
if (parameter != null)
{
parameter.CdParam = parameterDto.CdParam;
parameter.CdPlant = parameterDto.CdPlant;
parameter.ChDescr = parameterDto.ChDescr;
parameter.ChParam = parameterDto.ChParam;
parameter.NoOrder = parameterDto.NoOrder;
parameter.SwActive = parameterDto.SwActive;
parameter.SwAllowDel = parameterDto.SwAllowDel;
parameter.SwAllowNew = parameterDto.SwAllowNew;
parameter.SwAllowUpd = parameterDto.SwAllowUpd;
parameter.SwParent = parameterDto.SwParent;
await repository.UpdateAsync(parameter).ConfigureAwait(false);
var updatedParameter = await repository.GetFirstOrDefaultAsync(predicate: p => p.Id == parameterDto.Id);
var dto = mapper.Map<Domain.Definition.Parameter, ParameterDto>(updatedParameter);
return serviceResponseHelper.SetSuccess(dto);
}
return serviceResponseHelper.SetError<ParameterDto>(null, localize["ModelIsNotFound"], StatusCodes.Status400BadRequest, true);
}
catch (Exception ex)
{
return serviceResponseHelper.SetError<ParameterDto>(null, ex.InnerException?.Message ?? ex.Message, StatusCodes.Status500InternalServerError, true);
}
}
And this is my unit test for this method
[Fact]
public async Task ParameterService_Update_ReturnsUpdatedParameterDto()
{
// Arrange
var fixture = new Fixture();
var parameterDto = fixture.Create<ParameterDto>();
var parameter = fixture.Build<Domain.Definition.Parameter>()
.With(p => p.Id, parameterDto.Id) // Set the same Id as the parameterDto for matching
.Create();
var updatedParameter = fixture.Create<Domain.Definition.Parameter>();
var expectedResponse = new ServiceResponse<ParameterDto>(fixture.Create<ParameterDto>());
_repository.GetFirstOrDefaultAsync(Arg.Any<Expression<Func<Domain.Definition.Parameter, bool>>>(), null, null, default)
.Returns(parameter);
_repository.UpdateAsync(Arg.Any<Domain.Definition.Parameter>())
.Returns(Task.FromResult(updatedParameter));
_repository.GetFirstOrDefaultAsync(Arg.Any<Expression<Func<Domain.Definition.Parameter, bool>>>(), null, null, default)
.Returns(updatedParameter);
_mapper.Map<Domain.Definition.Parameter, ParameterDto>(Arg.Any<Domain.Definition.Parameter>())
.Returns(expectedResponse.Result);
// Act
var result = await _parameterService.Update(parameterDto);
// Assert
result.Should().BeEquivalentTo(expectedResponse);
result.Result.Should().BeEquivalentTo(expectedResponse.Result);
result.Should().NotBeNull();
result.IsSuccessful.Should().BeTrue();
}
Error is coming from in the main code
var parameter = await repository.GetFirstOrDefaultAsync(predicate: q => q.Id == parameterDto.Id);
答案1
得分: 0
我明白GetFirstOrDefaultAsync()方法需要所有参数按预期传递,而不是传递null参数。我填写了参数,这对我起作用了。
英文:
As i understand GetFirstOrDefaultAsync() methods need all parameters as expected instead of null parameters. I filled parameters and this worked for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论