这是一个BookDonation应用程序,我正在尝试使用Chai测试我的代码,我不知道该怎么做,如果我运行它会给我错误。
getBook(bookName):返回书本对象(如果存在),否则返回null
getBooksWIthPageCountMoreThanX(pageCount):返回pageCount大于给定pageCount的所有书籍。例如。如果用户使用pageCount = 1000调用该函数。它应该返回页数> 1000
的所有图书getAuthorBooks(author):返回由该特定作者创作的所有书籍。注意:有些书的作者不止一位。您也应该考虑这些因素,并将它们也退还。
getAuthorsBookCount():返回包含作者姓名和其创作书籍数量的地图。
BooksRepo类
class BooksRepo {
constructor() {
this.fse = require('fs-extra');
this.catalogFilePath = '../data/catalog.books.json';
}
async readFileAsync(filePath) {
let data = await this.fse.readFile(filePath);
let parsedData = await JSON.parse(data);
return parsedData;
}
async getBook(bookName) {
let books = await this.readFileAsync(this.catalogFilePath);
let book = books.find(b => b.title == bookName);
return book;
}
async getBooksWIthPageCountMoreThanX(pagecount){
let books = await this.readFileAsync(this.catalogFilePath);
let book = books.find(b => b.pageCount > pagecount);
return book;
}
async getAuthorBooks(author){
let books = await this.readFileAsync(this.catalogFilePath);
let book = books.find(b => b.authors.contains(author));
return book;
}
async getAuthorsBookCount(){
let authArray = [];
let authCountArray = [];
for (let i = 0; i < inputJson.length; i++) {
authArray = inputJson[i].authors;
for (let j = 0; j < authArray.length; j++) {
authCountArray[authArray[j]] = ((authCountArray[authArray[j]] === undefined) ? 0 : authCountArray[authArray[j]]) + 1;
}
}
return authCountArray[authName];
}
async getBooksbyCatagory(category){
let books = await this.readFileAsync(this.catalogFilePath);
let book = books.find(b => b.categories == category);
return book;
}
}
module.exports = BooksRepo ;
应用程序类
let expect = require('chai').expect
let BooksRepo = require('./BooksRepo');
let booksRepo = new BooksRepo();
describe("BooksRepo class test case" , ()=>{
it('search for book ', () => {
expect(booksRepo.getBook('Code Generation in Action')).to.equal()
});
it('get book with page count ', () => {
expect(booksRepo.getBooksWIthPageCountMoreThanX(1000)).to.equal(null)
});
it('get book with page count ', () => {
expect(booksRepo.getAuthorBooks('Charlie Collins')).to.equal('Charlie Collins')
});
it('get author and number of books he wrote ', () => {
expect(booksRepo.getAuthorsBookCount().to.equal()
});
it('get Books by Catagory ', () => {
expect(booksRepo.getBooksbyCatagory().to.equal()
});
});
目录文件:结构示例
[
{
"_id": 1,
"title": "Unlocking Android",
"isbn": "1933988673",
"pageCount": 416,
"publishedDate": {
"$date": "2009-04-01T00:00:00.000-0700"
},
"thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/ableson.jpg",
"shortDescription": "Unlocking Android: A Developer's Guide provides concise...",
"longDescription": "Android is an open source mobile phone platform based on the Linux operating ...",
"status": "PUBLISH",
"authors": [
"W. Frank Ableson",
"Charlie Collins",
"Robi Sen"
],
"categories": [
"Open Source",
"Mobile"
]
},