简体   繁体   English

如何将模拟数据从请求返回到Node中的Twitter Streaming API

[英]How do I return mock data from requests to the Twitter Streaming API in Node

I have a Node application that connects to the Twitter REST and streaming APIs. 我有一个连接到Twitter REST和流API的Node应用程序。 In order to test code that makes requests to the REST API, I can use Nock to intercept the HTTP request and return mock data like so: 为了测试向REST API发出请求的代码,我可以使用Nock截获HTTP请求并返回模拟数据,如下所示:

var nock = require('nock')

var mockData = [...]

nock('https://api.twitter.com/1.1')
.get('/search/tweets.json')
.reply(200, mockData)

My application also connects to the streaming API endpoint statuses/filter and performs some analysis on tweets received via the streaming API. 我的应用程序还连接到流API端点状态/过滤器,并对通过流API接收到的推文执行一些分析。 In Nock's README it states that you can pass a function to .query() that returns a stream, however I haven't been able to get this to work. 在Nock的自述文件中,它声明您可以将函数传递给.query() ,该函数返回流,但是我无法使其正常工作。

How would I return mock data from requests to this endpoint using Nock (or another library if necessary)? 如何使用Nock(或必要时使用另一个库)将模拟数据从请求返回到此端点? I'd ideally like to be able to send tweets to the stream in my tests whenever I need to, for example: 理想情况下,我希望能够在需要时将tweet发送到测试中的流,例如:

it('should correctly process tweets coming in on the streaming API', () => {
    var mockTweet = {...}

    sendMockTweetToStream(mockTweet)

    ...verify that the mock tweet was received and processed

})

I've found a way to do this in my tests without mocking the streaming API. 我在测试中找到了一种无需模拟流API的方法。

I'm using the NPM Twitter package to access the Twitter streaming API like so: 我正在使用NPM Twitter程序包访问Twitter流API,如下所示:

var client = new Twitter({
  consumer_key: twitterConsumerKey,
  consumer_secret: twitterConsumerSecret,
  access_token_key: twitterAccessTokenKey,
  access_token_secret: twitterAccessTokenSecret
})

stream = client.stream('statuses/filter', {track: 'something'})

stream.on('data', event => {
  // Do some processing of incoming tweets here
})

Instead of mocking the streaming API, I mocked the client object using my own stream with rewire, and set up a function to allow me to send it mock tweets whenever I want. 我没有模拟流API,而是使用自己的带有rewire的流模拟了client对象,并设置了一个函数,使我可以在需要时向其发送模拟推文。

const rewire = require('rewire')
const stream = rewire('stream')
const moduleToTest = require('somemodule')

const mockStream = new stream.Readable()
mockStream._read = () => {}
mockStream.destroy = () => {}

// Send a Tweet event to the mock stream
function writeTweetToStream (text) {
  mockStream.emit('data', {text: text})
}

describe('Module that uses the Twitter Streaming API', () => {
  beforeAll(() => {
    // client.stream should return our mock stream
    moduleToTest.__set__('client', {stream: () => { return mockStream }})
  })

  it('should process incoming tweets', () => {
    writeTweetToStream('This is a tweet #somehashtag')

    // Assert that the tweet was received and processed
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM