简体   繁体   English

如何在QUnit中进行真正的原子测试?

[英]How to have a real atomic test in QUnit?

Say that I have two classes called Book and Library as follow: 假设我有两个名为Book and Library类,如下所示:

var Book = function(title, author)
{
   this.title = title;
   this.author = author;
};

and

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;
};

My question is: How can I Unit testing the Library class using QUnit to have a real atomic and independent test? 我的问题是:如何使用QUnit对Library类进行单元测试以进行真正的原子和独立测试?

I wrote this test function but it has not satisfied me. 我写了这个测试功能,但它并没有让我满意。 It does not seem to be very atomic and independent since it mix several functions that I think should be tested independently. 它似乎不是非常原子和独立的,因为它混合了我认为应该独立测试的几个函数。 I'd like to know if there's a better way or I'm testing it good already. 我想知道是否有更好的方法,或者我已经测试好了。

test("test", function() {
   var library = new Library();

   deepEqual(library.getLibrary(), []);

   // Add book1
   var book1 = new Book("A book", "An author");
   deepEqual(library.addBook(book1), [book1]);
   deepEqual(library.getLibrary(), [book1]);

   // Add book2    
   book2 = new Result("A new book", "Another author");
   deepEqual(library.addBook(book2), [book2, book1]);
   deepEqual(library.getLibrary(), [book2, book1]]);

   // Remove book1
   deepEqual(library.removeResult(book1), [book2]);
   deepEqual(library.getLibrary(), [book2]);

   // Remove book2
   deepEqual(library.removeResult(book2), []);
   deepEqual(library.getLibrary(), []);
});

Avoid writing "omnibus" tests that depend on internal state of the unit you're testing. 避免编写依赖于您正在测试的单元的内部状态的“综合”测试。 Make fine-grained tests for specific behaviours , not state: 对特定行为进行细粒度测试,而不是说明:

test("addBooksReturnsLibrary", function(){
    var l = new Library();
    l.addBook(new Book(...));
    l.addBook(new Book(...));
    var addBooksRetval = l.addBook(new Book(...));
    deepEqual(addBooksRetval, l.getLibrary();
});

test("libraryContainsAddedBook"), function() {
    var l = new Library();
    var b = new Book(...);
    l.addBook(b);
    notEqual(-1, library.indexOf(b));
});

// etc and so forth

Heavy use of deepEqual seems like it's a testing smell more often than not. 大量使用deepEqual似乎更像是一种测试气味。 If you do need, say, the books to be in the library in a specific order, I'd say it's better to test for those ordering constraints specifically. 如果您确实需要按照特定顺序在图书馆中存放图书,我会说最好特别针对这些订购限制进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM