N取代物不';t替代方法

本文关键字:方法 取代 | 更新日期: 2023-09-27 18:00:35

我有我的测试目标类:

public class ApiClient
{
    private IRestClient authz_rest_client;
    private IRestClient api_rest_client;

    // Injection feature for testing
    internal ApiClient(IRestClient authz_rest_client, IRestClient api_rest_client)
    {
        this.authz_rest_client = authz_rest_client;
        this.api_rest_client = api_rest_client;
    }
    //...

因此,我注入我的替代RestSharp客户如下:

[TestFixture]
class AuthzApiClientTests
{
    private ApiClient api_client;
    private IRestClient authz_rest_client;
    private IRestClient api_rest_client;
    private IRestRequest request;
    private IRestResponse<OAuth2AuthzCodeResponse> response;
    [SetUp]
    public void SetUp()
    {
        this.authz_rest_client = NSubstitute.Substitute.For<IRestClient>();
        this.api_rest_client = NSubstitute.Substitute.For<IRestClient>();
        this.request = NSubstitute.Substitute.For<IRestRequest>();
        this.response = NSubstitute.Substitute.For<IRestResponse<OAuth2AuthzCodeResponse>>();
        this.authz_rest_client.Execute<OAuth2AuthzCodeResponse>(request).Returns(response);
        this.api_client = new ApiClient(this.authz_rest_client, this.api_rest_client);
        this.api_client.configure(
            "client_id",
            "user",
            "passwd"
        );
    }

然后,我写了一个测试:

    [Test]
    public void Should_ReturnCorrectRequestTokenServiceEndpoint()
    {
        response.StatusCode = HttpStatusCode.Unauthorized;
        response.Data = new OAuth2AuthzCodeResponse()
        {
            Error = StringEnum.GetStringValue(OAuth2ErrorTypes.invalid_client)  //CLIENT IS NOT REGISTERED ON LEST SYSTEM.
        };
        this.api_client.Invoking(c => c.GrantAuthorization())
            .ShouldThrow<OAuth2APIException>();
    }

正如您所看到的,我想测试我的ApiClient类的GrantAuthorization方法。这种方法是:

IRestRequest authzcode_request = new AuthzCodeRequest(
            this.settings.AuthzAuthorizeEndpoint,
            this.settings.ClientId,
            this.settings.ClientSecret,
            this.settings.User,
            this.settings.Password
        );
        IRestResponse<OAuth2AuthzCodeResponse> authzcode_response = this.authz_rest_client.Execute<OAuth2AuthzCodeResponse>(authzcode_request);
        this.check_response(authzcode_response);
        this.settings.AuthzCode = authzcode_response.Data.Code;
        this.settings.AuthzCodeExpirationThreshold = DateTime.Now.AddSeconds(authzcode_response.Data.Expires_in);

我的测试目标是"捕获"我的Execute<OAuth2AuthzCodeResponse>方法,以便返回我的替换响应。

问题是,当我执行测试并在这一行停止时,结果不是我之前设置的响应。

N取代物不';t替代方法

我很难理解完整的示例,但我确实注意到SetUp正在使用:

request = Substitute.For<IRestRequest>();
response = Substitute.For<IRestResponse<OAuth2AuthzCodeResponse>>();
authz_rest_client.Execute<OAuth2AuthzCodeResponse>(request).Returns(response);

这里的第三行表示,无论何时使用request实例调用authz_rest_client.Execute<OAuth2AuthzCodeResponse>(),它都将返回response。但是request似乎从未在代码中的任何地方使用过,所以Execute实际上永远不会返回response

类似以下行的内容将为任何对Execute<OAuth2AuthzCodeResponse>():的调用返回response

authz_rest_client.Execute<OAuth2AuthzCodeResponse>(null).ReturnsForAnyArgs(response);

但我也不确定你想要的是什么?相反,您可能希望截取单独的调用,例如当它收到类型为AuthzCodeRequest的请求时的特定响应。

已解决!

我试过用

this.authz_rest_client.Execute<OAuth2AuthzCodeResponse>(Arg.Any<IRestRequest>()).Returns(response);

这个解决方案类似于以前的答案方法。

然而,我不明白为什么不好好工作。。。