Created
May 28, 2018 04:39
-
-
Save lastday154/4e14c01ed15df9310cd2f9c1121c5971 to your computer and use it in GitHub Desktop.
unit test with sinon, mochai, chai, supertest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const app = require('../../app'); | |
| const request = require('supertest'); | |
| const sinon = require('sinon'); | |
| const { expect } = require('chai'); | |
| const ElasticSearch = require('../../models/elasticsearch'); | |
| const url = '/api/search?q=money&page_id=333&lang=en'; | |
| describe('GET /api/search', () => { | |
| let sandbox; | |
| beforeEach(() => { | |
| sandbox = sinon.createSandbox(); | |
| }); | |
| it('should response with 500 when no result found', (done) => { | |
| sandbox.stub(ElasticSearch, 'find').resolves(false); | |
| request(app) | |
| .get(url) | |
| .end((err, res) => { | |
| if (err) done(err); | |
| expect(res.statusCode).to.be.equal(500); | |
| expect(res.body).to.eql({ error: true }); | |
| expect(ElasticSearch.find.getCall(0).args).to.eql(['333', 'money', 'en']); | |
| done(); | |
| }); | |
| }); | |
| it('should response with 200 when faq exists', (done) => { | |
| const result = {"hits":{"hits":[{"_source":{"qid":"QA.008","q":"How do I get a Bitcoin address?","a":"Create by yourself"}},{"_source":{"qid":"QA.009","q":"How do I become millionaire before 30?","a":"By visualization"}},{"_source":{"qid":"QA.002","q":"How can I create or cancel a recurring transaction?","a":"How can I create a recurring transaction?"}}]},"suggest":{"q-suggest":[{"text":"money","offset":0,"length":5,"options":[{"_source":{"qid":"QA.009","q":"How do I become millionaire before 30?","a":"By visualization"}},{"_source":{"qid":"QA.0010","q":"How do I get a Bitcoin address?","a":"By visualization"}},{"_source":{"qid":"QA.006","q":"How do I receive digital currency from another wallet?","a":"You can use your Coinbase wallet to receive supported digital currencies"}}]}],"a-suggest":[{"text":"money","offset":0,"length":5,"options":[{"_source":{"qid":"QA.002","q":"How can I create or cancel a recurring transaction?","a":"How can I create a recurring transaction?"}}]}]}}; | |
| sandbox.stub(ElasticSearch, 'find').resolves(result); | |
| const data = [{"a":"By visualization","q":"How do I become millionaire before 30?","qid":"QA.009"},{"a":"By visualization","q":"How do I get a Bitcoin address?","qid":"QA.0010"},{"a":"You can use your Coinbase wallet to receive supported digital currencies","q":"How do I receive digital currency from another wallet?","qid":"QA.006"},{"a":"Create by yourself","q":"How do I get a Bitcoin address?","qid":"QA.008"},{"a":"How can I create a recurring transaction?","q":"How can I create or cancel a recurring transaction?","qid":"QA.002"}]; | |
| request(app) | |
| .get(url) | |
| .end((err, res) => { | |
| if (err) done(err); | |
| expect(res.statusCode).to.be.equal(200); | |
| expect(res.body).to.eql({ data: data }); | |
| expect(ElasticSearch.find.getCall(0).args).to.eql(['333', 'money', 'en']); | |
| done(); | |
| }); | |
| }); | |
| afterEach(function () { | |
| sandbox.restore(); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment