【发布时间】:2017-03-13 07:07:43
【问题描述】:
这是我想在模拟数据库中测试的update 函数
import Book from '../model/book';
function bookRepository(db) {
this.db = db;
};
bookRepository.prototype.update = async function(id, data) {
return await Book.findOneAndUpdate({ _id: id }, { $set: data });
}
export default bookRepository;
这是我为它写的测试脚本
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
const expect = chai.expect;
import app from '../../server';
import bookRepo from '../../repository/book';
const Book = new bookRepo(app.db);
describe('Test repository: book', () => {
describe('update', () => {
let id;
beforeEach(async() => {
let book = {
name: 'Records of the Three Kingdoms',
type: 'novel',
description: 'History of the late Eastern Han dynasty (c. 184–220 AD) and the Three Kingdoms period (220–280 AD)',
author: 'Luo Guanzhong',
language: 'Chinese'
};
let result = await Book.insert(book);
id = await result.id;
return;
});
it('Update successfully', async() => {
let data = {
type: 'history',
author: 'Chen Shou'
};
let result = await Book.update(id, data);
await expect(result).to.be.an('object');
await expect(result.type).to.be.equal('history');
return expect(result.author).to.be.equal('Chen Shou');
});
});
});
我收到了这个错误
AssertionError: expected 'novel' to equal 'history'
+ expected - actual
当我检查模拟数据库时,它确实更新了数据,但为什么它的断言失败了?完成await调用后应该已经更新了
【问题讨论】:
-
console.log(result)给你什么? -
@lonesomeday 和
book一模一样,好像还没更新一样
标签: javascript node.js unit-testing ecmascript-6 async-await