提交 fc503e11 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

use sinon.sandbox for stub

上级 69762e17
......@@ -58,15 +58,17 @@ test('Puppet smoke testing', async t => {
test('login/logout events', sinonTest(async function (t: test.Test) {
sinon.stub(Contact, 'findAll')
const sandbox = sinon.sandbox.create()
sandbox.stub(Contact, 'findAll')
.onFirstCall().resolves([])
.onSecondCall().resolves([1])
.resolves([1, 2])
sinon.stub(Event, 'onScan') // block the scan event to prevent reset logined user
sandbox.stub(Event, 'onScan') // block the scan event to prevent reset logined user
sinon.stub(Bridge.prototype, 'getUserName').resolves('mockedUserName')
sinon.stub(PuppetPuppeteer.prototype, 'getContact') .resolves({
sandbox.stub(Bridge.prototype, 'getUserName').resolves('mockedUserName')
sandbox.stub(PuppetPuppeteer.prototype, 'getContact') .resolves({
NickName: 'mockedNickName',
UserName: 'mockedUserName',
})
......@@ -110,6 +112,7 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
} catch (e) {
t.fail(e)
} finally {
sandbox.restore()
t.end()
}
}))
......@@ -24,18 +24,17 @@ import * as sinon from 'sinon'
import cloneClass from 'clone-class'
import Profile from '../profile'
import Wechaty from '../wechaty' // `Wechaty` need to be imported before `Puppet`
import PuppetPuppeteer from './puppet-puppeteer'
import PuppeteerContact from './puppeteer-contact'
import Profile from '../profile'
import Wechaty from '../wechaty'
test('Contact smoke testing', async t => {
// tslint:disable-next-line:variable-name
const MyContact = cloneClass(PuppeteerContact)
MyContact.puppet = new PuppetPuppeteer({
const puppet = new PuppetPuppeteer({
profile: new Profile(),
wechaty: new Wechaty(),
})
......@@ -45,19 +44,24 @@ test('Contact smoke testing', async t => {
const NickName = 'NickNameTest'
const RemarkName = 'AliasTest'
sinon.stub((MyContact.puppet as PuppetPuppeteer), 'getContact', function(id: string) {
const sandbox = sinon.sandbox.create()
sandbox.stub(puppet, 'getContact')
.callsFake(function(id: string) {
return new Promise<any>((resolve, reject) => {
if (id !== UserName) return resolve({})
setTimeout(() => {
return resolve({
UserName: UserName,
NickName: NickName,
UserName: UserName,
NickName: NickName,
RemarkName: RemarkName,
})
}, 200)
}, 10)
})
})
MyContact.puppet = puppet
const c = new MyContact(UserName)
t.is(c.id, UserName, 'id/UserName right')
......@@ -66,8 +70,7 @@ test('Contact smoke testing', async t => {
t.is(r.name(), NickName, 'NickName set')
t.is(r.alias(), RemarkName, 'should get the right alias from Contact')
const s = r.toString()
t.is(typeof s, 'string', 'toString()')
sandbox.restore()
// const contact1 = await Contact.find({name: 'NickNameTest'})
// t.is(contact1.id, UserName, 'should find contact by name')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册