我正在学习xUnit Test,并且在运行测试用例时遇到了此类错误:
System.NullReferenceException : Object reference not set to an instance of an object.
我在网上搜索,但似乎没有任何效果。 以下是我的代码:
Controller
public class AuthorsController : ControllerBase
{
private readonly IRepositoryWrapper Wrapper;
private readonly ILoggerManager Logger;
public AuthorsController(ILoggerManager logger, IRepositoryWrapper wrapper)
{
Wrapper = wrapper;
Logger = logger;
}
public AuthorsController(IRepositoryWrapper wrapper)
{
Wrapper = wrapper;
}
[HttpGet]
public async Task<IActionResult> GetAuthors()
{
try
{
var authors = await Wrapper.Author.GetAllAuthorsAsync();
Logger.LogInfo($"Returned all authors from database.");
return Ok(authors);
}
catch (Exception ex)
{
Logger.LogError($"Error: {ex}");
return StatusCode(500, "Internal server error");
}
}
[HttpGet("{id}")]
public async Task<IActionResult> GetAuthorByID(int id)
{
try
{
var author = await Wrapper.Author.GetAuthorByIdAsync(id);
if (author == null)
{
Logger.LogError($"Author with ID {id} NOT FOUND");
return NotFound();
}
else
{
Logger.LogInfo($"Returned author with ID {id}");
return Ok(author);
}
}
catch (Exception ex)
{
Logger.LogError($"Error: {ex}");
return StatusCode(500, "Internal server error");
}
}
}
RepositoryWrapper Class
包含2个存储库,分别是AuthorRepository
和BookRepository
,由于测试用例与作者有关,因此我只发布了AuthorRepository
代码。
public AuthorRepository(RepositoryContext repositoryContext)
: base(repositoryContext)
{ }
public async Task<IEnumerable<Author>> GetAllAuthorsAsync()
{
var authors = await FindAll()
.OrderBy(a => a.AuthorName)
.Include(a => a.Books)
.ToListAsync();
return authors;
}
public async Task<Author> GetAuthorByIdAsync(int AuthorID)
{
var author = await FindByCondition(b => b.AuthorID
.Equals(AuthorID))
.Include(a => a.Books)
.SingleOrDefaultAsync();
return author;
}
xUnit Test Case
public class AuthorsTest
{
//Arrange
private readonly AuthorsController Controller;
readonly RepositoryWrapper repository;
public AuthorsTest()
{
Controller = new AuthorsController(repository);
}
//Test 1 - Return Ok Response when get authors
[Fact]
public void GetAuthors_OkResult()
{
//Act
var okResult = Controller.GetAuthors().Result;
//Assert
Assert.IsType<OkObjectResult>(okResult);
}
//Test 2 - Return all authors when get authors
[Fact]
public void GetAuthors_ReturnsAll()
{
//Act
var okResult = Controller.GetAuthors().Result as OkObjectResult;
//Assert
var authors = Assert.IsType<List<Author>>(okResult.Value);
Assert.Equal(2, authors.Count());
}
//Test 3 - Return NotFound Response on invalid AuthorID
[Fact]
public void GetAuthorByID_ReturnNotFound()
{
//Act
var notFoundResult = Controller.GetAuthorByID(10);
//Assert
Assert.IsType<NotFoundResult>(notFoundResult.Result);
}
//Test 4 - Return Ok Response when get author by ID
[Fact]
public void GetAuthorByID_ReturnOkResult()
{
//Act
var okResult = Controller.GetAuthorByID(6);
//Assert
Assert.IsType<OkObjectResult>(okResult.Result);
}
//Test 5 - Return selected author when get author by ID
[Fact]
public void GetAuthorByID_ReturnAuthor()
{
//Act
var okResult = Controller.GetAuthorByID(6).Result as OkObjectResult;
//Assert
Assert.IsType<Author>(okResult.Value);
Assert.Equal(6, (okResult.Value as Author).AuthorID);
}
}
请帮助解决错误,因为我不知道如何发生,因为我一直在遵循网络上的教程。谢谢。