Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8650539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:50:50+00:00 2026-06-12T13:50:50+00:00

Say that I have two classes called Book and Library as follow: var Book

  • 0

Say that I have two classes called Book and Library as follow:

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?

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(), []);
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:50:51+00:00Added an answer on June 12, 2026 at 1:50 pm

    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. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two classes and have a requirement that the primary key property
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Let's say I have two tables that look like this: TH TH TH TH
Say I have two classes like this: class A{ private static Random random =
Let just say that we have two classes, A and B . Here is
Say I have two classes, in two different headers, called: class TestA { public:
Let's say that I have two classes A and B. class A { private:
Say that I have two figures in matplotlib, with one plot per figure: import
Lets say that I have two strings: The System is in halt state. The
Let's say that I have two libraries (A and B), and each has one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.