首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSubstitute不会替换方法

NSubstitute不会替换方法
EN

Stack Overflow用户
提问于 2015-07-01 21:07:02
回答 2查看 1K关注 0票数 1

我有我的测试目标类:

代码语言:javascript
复制
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客户端:

代码语言:javascript
复制
[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"
        );
    }

然后,我编写了一个测试:

代码语言:javascript
复制
    [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方法。此方法为:

代码语言:javascript
复制
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>方法,以便返回被替换的响应。

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

EN

回答 2

Stack Overflow用户

发布于 2015-07-02 07:10:03

我在完成整个示例时遇到了问题,但我确实注意到了SetUp使用的一些东西:

代码语言:javascript
复制
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

代码语言:javascript
复制
authz_rest_client.Execute<OAuth2AuthzCodeResponse>(null).ReturnsForAnyArgs(response);

但我也不确定这就是你想要的?相反,您可能希望清除单个调用,例如当它获得类型为AuthzCodeRequest的请求时的特定响应。

票数 1
EN

Stack Overflow用户

发布于 2015-07-02 19:49:53

解决了!

我试着这样做:

代码语言:javascript
复制
this.authz_rest_client.Execute<OAuth2AuthzCodeResponse>(Arg.Any<IRestRequest>()).Returns(response);

此解决方案类似于前面的答案方法。

然而,我不明白为什么不永久地工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31162234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档