在Mocha中使用sinon创建请求存根 [英] Creating request stub with sinon in mocha

查看:89
本文介绍了在Mocha中使用sinon创建请求存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mocha来测试某些类,并且我需要创建一个request库的存根.

I'm using mocha to test some classes and I need to create a stub of request library.

我正在使用sinon,并且能够创建request.get方法的存根,但无法创建request方法的存根(http调用尝试连接到服务器).如我所读,request.getrequest的别名,但是当我对request.get进行存根处理时,它对request调用没有任何作用.

I'm using sinon, and I'm able to create a stub of the request.get method but I'm not able to create a stub of the request method (the http calls try to connect to a server). As I have read, request.get is an alias for request but when I stub request.get it has no effect over request calls.

此代码有效(使用request.get):

在测试中:

request = require 'request'

describe "User test", ->
  user = {}
  before (done) ->
    user = new test.user('Ander', 18)    
    sinon.stub(request, 'get').yields(null, {statusCode: 200}, 'foo')        
    done()
  after (done) ->
    request.get.restore()
    done()
  it "testing server response", ->
    user.getData().should.equal 'ander'

在源中:

request = require 'request'

class User
  contructor(@name, @age): ->
  getData: ->
    mydata = ''
    request.get 'http://127.0.0.1:8080/', (err, response, body) ->        
      if not err and response.statusCode == 200
        mydata = body
      else
        err = throw new Error "Errorea"
    mydata

但这不起作用(尝试连接到提供的网址):

But this doesn't work (tries to connect to the supplied url):

在测试中:

request = require 'request'

describe "User test", ->
  user = {}
  before (done) ->
    user = new test.user('Ander', 18)    
    sinon.stub(request, 'Request').yields(null, {statusCode: 200}, 'foo')
    #Creating the stub this way, doesn't work neither
    #sinon.stub(request, 'get').yields(null, {statusCode: 200}, 'foo')
    done()
  after (done) ->
    request.Request.restore()
    done()
  it "testing server response", ->
    user.getData().should.equal 'ander'

在源中:

request = require 'request'

class User
  contructor(@name, @age): ->
  getData: ->
    mydata = ''
    request 'http://127.0.0.1:8080/', (err, response, body) ->        
      if not err and response.statusCode == 200
        mydata = body
      else
        err = throw new Error "Errorea"
    mydata

哪种方式为request呼叫创建存根是正确的方法?哪种方法存根?

Which is the right way to create a stub for request call? Which is the method to be stubed?

推荐答案

尽管request是一个很棒的库,但它不是一个结构良好的API的好例子.而且由于模块request被定义为具有其他方法的函数(类似于express),据我所知,您无法使用sinon为函数request创建存根.

Although request is a great library, it is not a good example of well structured API. And because module request is defined as a function with additional methods (similarly like express), as what I know you can't create stub for function request with sinon.

最好的办法是避免在代码中使用request函数,而仅使用request.getrequest.post等,可以很容易地对其进行存根.

The best thing you can do is to avoid to use request function in your code and use only request.get, request.post, etc., which you can easily stub.

在第二个示例中为Request创建存根没有帮助,因为Request不是一种方法,请参见源代码.

Creating stub for Request in your second example doesn't help because Request is not a method, see source code.

这篇关于在Mocha中使用sinon创建请求存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆