xUnit,最小起订量 - 测试 .Net 核心存储库
xUnit, Moq - test .Net Core Repository
我正在学习用 xUnit 和 Moq 编写单元测试,但遇到了一些问题。我合写了 2 个测试,我添加了一个类别并下载了所有测试,通过 Assert 或任何它们进行检查。在第二种情况下,我也添加了类别,并获得了添加类别的详细信息,不幸的是我无法显示下载类别的详细信息,这是 TestCategoryDetails 测试。我做错了什么?
using Moq;
using relationship.Models;
using Xunit;
using Xunit.Abstractions;
namespace Testy
{
public class UnitTest1
{
private readonly ITestOutputHelper _output;
public UnitTest1(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void TestCategoryList()
{
var categoryMock = new Mock<ICategoryRepository>();
var contextMock = new Mock<AppDbContext>();
categoryMock.Setup(x => x.AddCategory(new GameCategory { Id= 1, Name = "Tester" }));
var result = categoryMock.Object;
Assert.NotNull(result.GameCategory());
}
[Fact]
public void TestCategoryDetails()
{
var categoryMock = new Mock<ICategoryRepository>();
var contextMock = new Mock<AppDbContext>();
categoryMock.Setup(x => x.AddCategory(new GameCategory { Id = 1, Name = "Tester" }));
var result = categoryMock.Object;
var categoryDetails = result.GetDetails(1);
Assert.NotNull(categoryDetails);
}
}
}
总的来说,我想通过检查如何添加、编辑、删除、下载所选类别的所有类别和详细信息来测试我的存储库,不幸的是我什么也没做。
您在做什么,是在尝试测试存储库抽象的模型。但是您想测试您的实施。
使用数据库上下文进行测试的有效方法是在内存提供程序中使用真实上下文。详情请见:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/
最后可能看起来像这样(第二次测试):
...
[Fact]
public void TestCategoryDetails()
{
// arrange
var categoryRepository = new CategoryRepository(GetContextWithInMemoryProvider());
// act
categoryRepository.AddCategory(new GameCategory { Id = 1, Name = "Tester" });
var categoryDetails = categoryRepository.GetDetails(1);
// assert
Assert.NotNull(categoryDetails);
}
private AppDbContext GetContextWithInMemoryProvider()
{
// create and configure context
// see: https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/
}
...
我正在学习用 xUnit 和 Moq 编写单元测试,但遇到了一些问题。我合写了 2 个测试,我添加了一个类别并下载了所有测试,通过 Assert 或任何它们进行检查。在第二种情况下,我也添加了类别,并获得了添加类别的详细信息,不幸的是我无法显示下载类别的详细信息,这是 TestCategoryDetails 测试。我做错了什么?
using Moq;
using relationship.Models;
using Xunit;
using Xunit.Abstractions;
namespace Testy
{
public class UnitTest1
{
private readonly ITestOutputHelper _output;
public UnitTest1(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void TestCategoryList()
{
var categoryMock = new Mock<ICategoryRepository>();
var contextMock = new Mock<AppDbContext>();
categoryMock.Setup(x => x.AddCategory(new GameCategory { Id= 1, Name = "Tester" }));
var result = categoryMock.Object;
Assert.NotNull(result.GameCategory());
}
[Fact]
public void TestCategoryDetails()
{
var categoryMock = new Mock<ICategoryRepository>();
var contextMock = new Mock<AppDbContext>();
categoryMock.Setup(x => x.AddCategory(new GameCategory { Id = 1, Name = "Tester" }));
var result = categoryMock.Object;
var categoryDetails = result.GetDetails(1);
Assert.NotNull(categoryDetails);
}
}
}
总的来说,我想通过检查如何添加、编辑、删除、下载所选类别的所有类别和详细信息来测试我的存储库,不幸的是我什么也没做。
您在做什么,是在尝试测试存储库抽象的模型。但是您想测试您的实施。
使用数据库上下文进行测试的有效方法是在内存提供程序中使用真实上下文。详情请见: https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/
最后可能看起来像这样(第二次测试):
...
[Fact]
public void TestCategoryDetails()
{
// arrange
var categoryRepository = new CategoryRepository(GetContextWithInMemoryProvider());
// act
categoryRepository.AddCategory(new GameCategory { Id = 1, Name = "Tester" });
var categoryDetails = categoryRepository.GetDetails(1);
// assert
Assert.NotNull(categoryDetails);
}
private AppDbContext GetContextWithInMemoryProvider()
{
// create and configure context
// see: https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/
}
...