小编And*_*ius的帖子

.NET core 使用 Xunit + Autofixture + Moq 编写更好的单元测试

在 .NET Core 中进行单元测试,我使用 Xunit、Moq 和 Autofixture。但即使有了它们,我发现我的单元测试变得复杂并且需要时间。

也许有人可以告诉我是否有任何方法可以缩小这个测试的规模?

[Fact]
public async Task Verify_NotAuthorised_NoServiceSendInvoked()
{
    // Arrange
    var fixture = new Fixture()
        .Customize(new AutoMoqCustomization());

    var sut = fixture.Create<VerificationService>();

    var mockApiBuilder = fixture.Freeze<Mock<IApiEntityBuilder>>();
    //init mocked mockSendServiceOne, so later I could check if it was invoked or not
    var mockSendServiceOne = fixture.Freeze<Mock<ISendServiceOne>>();

    mockApiBuilder.Setup(x => x.Verification(It.IsAny<string>(), It.IsAny<string>()))
        .Returns(fixture.Create<VerificationEntity>());

    var call = fixture.Freeze<Mock<ISendServiceTwo>>();
    call.Setup(x => x.IsSuccessful()).Returns(false);

    // Act
    await sut.Verify(fixture.Create<string>(), fixture.Create<string>());

    // Assert
    mockSendServiceOne.Verify(x => x.Call(It.IsAny<SendServiceOneEntity>()), Times.Never);
}
Run Code Online (Sandbox Code Playgroud)

测试方法本身

public async Task<CreatedEntity> Verify(string dataOne, string …
Run Code Online (Sandbox Code Playgroud)

unit-testing moq xunit autofixture .net-core

4
推荐指数
1
解决办法
7958
查看次数

标签 统计

.net-core ×1

autofixture ×1

moq ×1

unit-testing ×1

xunit ×1