-
-
Save consoledotblog/d044ab712110a76d0344 to your computer and use it in GitHub Desktop.
Building Applications with TypeScript - Snippet 20
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
/* | |
* Import the libaries that we're using. | |
* This is a test of API code, so any libraries | |
* specified in package.json should be available | |
* for import here. | |
*/ | |
import chai = require('chai'); | |
import chaiAsPromised = require("chai-as-promised"); | |
import sinon = require('sinon'); | |
// Bring in the Mongo client since we're going to stub it out. | |
import Mongo = require('mongodb'); | |
// Bring in the class we want to test. | |
import {EnvDataConfig, MongoDataDriver} from '../api/db'; | |
// Augment our pre-orders and our test expectations. | |
chai.use(chaiAsPromised); | |
var expect = chai.expect; | |
describe('MongoDataDriver', () => { | |
var driver, | |
connectStub; | |
beforeEach(() => { | |
driver = new MongoDataDriver(EnvDataConfig.getInstance()); | |
connectStub = sinon.stub(Mongo.MongoClient, 'connect'); | |
}); | |
afterEach(() => { | |
/* | |
* When you stub a method using Sinon, it attaches | |
* a 'restore' method that you'll need to call after | |
* your test finishes as part of cleanup. Of course, | |
* that's not part of the type associated with MongoClient, | |
* so the TypeScript compiler will complain about this | |
* unless we access the restore method using an object key. | |
*/ | |
Mongo.MongoClient.connect['restore'](); | |
}); | |
describe('getConnection', () => { | |
it('should reject the promise if an error occurs on connect', () => { | |
connectStub.callsArgWith(1, {}); | |
expect(driver.getConnection()).to.eventually.be.rejected; | |
}); | |
it('should resolve the promise if no error occurred on connect', () => { | |
connectStub.callsArgWith(1, undefined); | |
expect(driver.getConnection()).to.eventually.be.fulfilled; | |
}); | |
it('should resolve the promise with the connection from the Mongo driver', () => { | |
var expected = {}; | |
connectStub.callsArgWith(1, undefined, expected); | |
expect(driver.getConnection()).to.eventually.equal(expected); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment