说我有两个类叫做Book及Library如下:
var Book = function(title, author)
{
this.title = title;
this.author = author;
};
Run Code Online (Sandbox Code Playgroud)
和
var Library = function()
{
var dbName = 'test';
this.getLibrary = function() {
return JSON.parse(window.localStorage.getItem(dbName));
};
this.save = function(library) {
window.localStorage.setItem(dbName, JSON.stringify(library));
};
}
Library.prototype.addBook = function(book) {
var library = this.getLibrary();
library.push(book);
this.save(library);
return library;
};
Library.prototype.removeBook = function(book) {
var library = this.getLibrary();
// Find and delete the right book
this.save(library);
return library;
};
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何Library使用QUnit对类进行单元测试以进行真正的原子和独立测试?
我写了这个测试功能,但它并没有让我满意.它似乎不是非常原子和独立的,因为它混合了我认为应该独立测试的几个函数.我想知道是否有更好的方法,或者我已经测试好了.
test("test", …Run Code Online (Sandbox Code Playgroud)