简体   繁体   English

单元测试应该失败

[英]Unit test should be failing

I'm trying to write a unit test for GenerateTokenAsync() 我正在尝试为GenerateTokenAsync()编写单元测试

public async Task<string> GenerateTokenAsync()
{
    Verify.IsNotNullOrWhiteSpace(ClientId, nameof(ClientId));
    Verify.IsNotNullOrWhiteSpace(ClientSecret, nameof(ClientSecret));
    Verify.IsNotNullOrWhiteSpace(TenantId, nameof(TenantId));

    var clientCredential = new ClientCredential(ClientId, ClientSecret);
    var context = new AuthenticationContext(Constants.AuthenticationContextUrl + TenantId);
    var authResult = await context.AcquireTokenAsync(Constants.AuthResultUrl, clientCredential);

    return authResult?.AccessToken;
}

and there are 3 properties that must have values. 并且有3个必须具有值的属性。

I'm expecting Verify.IsNotNullOrWhiteSpace() to throw an exception but it is not. 我期望Verify.IsNotNullOrWhiteSpace()引发异常,但事实并非如此。

This is a static helper class that throws an ArgumentException() . 这是一个静态助手类,该类抛出ArgumentException()

The test should be failing since no values have been provided for all 3 properties. 测试应该失败,因为没有为所有3个属性提供值。

I have tried to change the unit test, but not working. 我试图更改单元测试,但无法正常工作。

[TestMethod]
public async Task GenerateTokenAsync_WhenExecuted_GeneratesToken()
{
    // Arrange
    var mockAzureClient = new Mock<IAzureClient>();
    mockAzureClient.Setup(m => m.GenerateTokenAsync()).ReturnsAsync(Guid.NewGuid().ToString("N"));

    // Act
    var token = await mockAzureClient.Object.GenerateTokenAsync();

    // Assert
    token.Should().NotBeNullOrWhiteSpace();
}

This might be an XY problem as it looks like you are mocking the subject under test. 这可能是一个XY问题,因为您似乎在嘲笑被测对象。

In this case I see no need for using Mock to Test the above function. 在这种情况下,我认为无需使用Mock来测试上述功能。 Create an instance and invoke the member under test 创建一个实例并调用被测成员

[TestMethod]
public async Task GenerateTokenAsync_WhenExecuted_WithoutProperties_ShouldFail() {
    // Arrange
    IAzureClient azureClient = new AzureClient(); //Assuming name here
    ArgumentException actual = null;

    // Act
    try {
        var token = await azureClient.GenerateTokenAsync();
    } catch(ArgumentException ex) {
        actual = ex;
    }

    // Assert
    actual.Should().NotBeNull();
}

You are basically testing an external SDK which is not something you should concern yourself with. 您基本上是在测试外部SDK,这不是您应该关注的事情。

An SDK can easily change, methods and ways of doing things can change as well and you have no control over that. SDK可以轻松更改,做事的方法和方式也可以更改,您对此无能为力。 Chances are this code won't work as soon as anything changes. 一旦任何更改,此代码就有可能无法正常工作。

That code is more than likely covered by tests provided by the SDK authors, in which case what exactly are you achieving with this thing? SDK作者提供的测试很可能涵盖了该代码,在这种情况下,您究竟要实现什么? I can't call it a unit test because it isn't one. 我不能称其为单元测试,因为它不是一个。 It's an integration test and not a very useful one either. 这是一个集成测试,也不是一个非常有用的测试。

I would suggest you focus on testing the code which calls into the SDK. 我建议您专注于测试调用SDK的代码。 Make sure you do not call the SDK with empty parameters, that code is yours and you can control it. 确保您不使用空参数调用SDK,该代码是您自己的并且可以控制它。 Test that and leave the SDK testing to its own authors. 进行测试,然后将SDK测试留给自己的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM