首页 \ 问答 \ 是否可以嘲笑.NET HttpWebResponse?(Is it possible to mock out a .NET HttpWebResponse?)

是否可以嘲笑.NET HttpWebResponse?(Is it possible to mock out a .NET HttpWebResponse?)

我有一个集成测试,从第三方服务器获取一些json结果。 这真的很简单,效果很好。

我希望能够停止这个服务器,并使用Moq (或任何Mocking库,如ninject等)来劫持并强制返回结果。

这可能吗?

以下是一些示例代码:

public Foo GoGetSomeJsonForMePleaseKThxBai()
{
    // prep stuff ...

    // Now get json please.
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("Http://some.fancypants.site/api/hiThere);
    httpWebRequest.Method = WebRequestMethods.Http.Get;

    string responseText;

    using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
    {
        using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
        {
            json = streamReader.ReadToEnd().ToLowerInvariant();
        }
    }

    // Check the value of the json... etc..
}

当然这个方法是从我的测试中调用的。

我在想,也许我需要传递这个方法(或类的一个属性)?一个嘲笑的httpWebResponse或某事,但不太确定,如果这样。 此外,响应是来自httpWebRequest.GetResponse()方法的输出。所以也许我只需要传递一个嘲笑的HttpWebRequest

任何关于某些示例代码的建议将是最为aprrci!


i've got an integration test that grabs some json result from a 3rd party server. It's really simple and works great.

I was hoping to stop actually hitting this server and using Moq (or any Mocking library, like ninject, etc) to hijack and force the return result.

is this possible?

Here is some sample code :-

public Foo GoGetSomeJsonForMePleaseKThxBai()
{
    // prep stuff ...

    // Now get json please.
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("Http://some.fancypants.site/api/hiThere);
    httpWebRequest.Method = WebRequestMethods.Http.Get;

    string responseText;

    using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
    {
        using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
        {
            json = streamReader.ReadToEnd().ToLowerInvariant();
        }
    }

    // Check the value of the json... etc..
}

and of course, this method is called from my test.

I was thinking that maybe I need to pass into this method (or a property of the class?) a mocked httpWebResponse or something but wasn't too sure if this was the way. Also, the response is a output from an httpWebRequest.GetResponse() method .. so maybe I just need to pass in a mocked HttpWebRequest ?.

any suggestions with some sample code would be most aprreciated!


原文:https://stackoverflow.com/questions/9823039
更新时间:2019-11-20 20:45

最满意答案

好吧,你做错了几件事。

  1. 您需要指定要注入的依赖项作为参数。 例如, require(["obr/obr.platcom"], function() {除非你指定如何调用所需的模块,否则不会做太多。你应该需要这个:

    require(["obr/obr.platcom"], function( obr ) {
    

    这样,您就知道所需对象在哪个变量中。

  2. obr.js变量位于全局范围内。 您需要将它们包装在requiredefine函数调用中。 以下方法可行:

    define(function() {
        var obr = {};
        obr.hola = function() {};
        return obr;
    });
    

    您可能已经注意到上一个文件有些问题。

  3. 如果您希望在某处导入模块,则必须对其进行定义 。 所以你必须使用define函数,而不是require函数。 而且define函数必须返回一个对象。 这是一个固定的obr.platcom.js文件:

    // If you don't use "define" for the obr.js file too, this won't work
    define(['obr'], function( obr ) {
        obr.platcom = function() {};
    
        // Don't forget to return the obr object, or the require of
        // the main file won't return anything
        return obr;
    });
    

这样,事情就是以正确的方式完成的。 或者至少,require.js希望你做的事情。

我希望这能告诉你如何有效地使用require.js来轻松地分离模块中的代码:)


Alright, you've done several things wrong.

  1. You need to specify as an argument the dependency you're injecting. For example, require(["obr/obr.platcom"], function() { won't do much unless you specify how the required module can be called. You should need this:

    require(["obr/obr.platcom"], function( obr ) {
    

    This way, you know in which variable your required object is.

  2. The obr.js variables are in the global scope. You need to wrap them in a require or define function call. The following would work:

    define(function() {
        var obr = {};
        obr.hola = function() {};
        return obr;
    });
    

    You may have noticed some things that are wrong with your last file.

  3. If you want your module to be imported somewhere, you have to define it. So you have to use the define function, not the require one. And the define function must return an object. Here is a fixed obr.platcom.js file:

    // If you don't use "define" for the obr.js file too, this won't work
    define(['obr'], function( obr ) {
        obr.platcom = function() {};
    
        // Don't forget to return the obr object, or the require of
        // the main file won't return anything
        return obr;
    });
    

This way, things are done the correct way. Or at least, the way require.js wants you to do stuff.

I hope this reveals you how require.js can be effectively used to easily separate your code in modules :)

相关问答

更多