是否可以嘲笑.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 anhttpWebRequest.GetResponse()
method .. so maybe I just need to pass in a mockedHttpWebRequest
?.any suggestions with some sample code would be most aprreciated!
原文:https://stackoverflow.com/questions/9823039
最满意答案
好吧,你做错了几件事。
您需要指定要注入的依赖项作为参数。 例如,
require(["obr/obr.platcom"], function() {
除非你指定如何调用所需的模块,否则不会做太多。你应该需要这个:require(["obr/obr.platcom"], function( obr ) {
这样,您就知道所需对象在哪个变量中。
obr.js
变量位于全局范围内。 您需要将它们包装在require
或define
函数调用中。 以下方法可行:define(function() { var obr = {}; obr.hola = function() {}; return obr; });
您可能已经注意到上一个文件有些问题。
如果您希望在某处导入模块,则必须对其进行定义 。 所以你必须使用
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.
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.
The
obr.js
variables are in the global scope. You need to wrap them in arequire
ordefine
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.
If you want your module to be imported somewhere, you have to define it. So you have to use the
define
function, not therequire
one. And thedefine
function must return an object. Here is a fixedobr.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 :)
相关问答
更多-
Require.js嵌套需要(Require.js nested requires)[2023-10-15]
好吧,你做错了几件事。 您需要指定要注入的依赖项作为参数。 例如, require(["obr/obr.platcom"], function() {除非你指定如何调用所需的模块,否则不会做太多。你应该需要这个: require(["obr/obr.platcom"], function( obr ) { 这样,您就知道所需对象在哪个变量中。 obr.js变量位于全局范围内。 您需要将它们包装在require或define函数调用中。 以下方法可行: define(function() { var ... -
define()只能用于定义一个模块。 对于上述示例,如果一段代码应该被动态加载,那么使用require()更合适: functionA.js functionA(){ require(['functionC'],function(functionC){ //use funcC in here to call functionC }); } 一些注释: require([])是异步的,所以如果functionA的调用者期望该函数返回一个值,那么可能会出现错误。 如果functionA接受一 ...