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 920119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:39:28+00:00 2026-05-15T18:39:28+00:00

Suppose I need to add unit tests for the following class from legacy code

  • 0

Suppose I need to add unit tests for the following class from legacy code (that has no unit test for now). It is just a simple map or dictionary.

function Map(...) { ... }
Map.prototype.put = function (key, value) {
    // associate the value with the key in this map
}
Map.prototype.get = function (key) {
    // return the value to which the specified key is mapped, or undefined 
    // if this map contains no mapping for the key
}
Map.prototype.equals = function (obj) { ... }
// ... and more bound functions

It seems there is no way to test only one function at a time. You cannot test get() without calling put(), for example. How do I unit test this?

  • 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-05-15T18:39:28+00:00Added an answer on May 15, 2026 at 6:39 pm

    Each method has a contract, explicit or implicit. Map.put() takes some kind of input and mutates something internal or external to Map. In order to test that function, your test needs access to what is mutated. If it is internal and not exposed externally, your test must either exist inside the Map class, the state must be exposed, or the mutateable state structure must be injected into the class in a way that external access remains possible:
    ie:

    /*Definition*/
    
    function MockRepository() { /*implementation of the repository*/ }
    function Map(repository) { /* store the repository */ }
    Map.prototype.put = function() { /* mutate stuff in the repository */ }
    
    /*Instantiation/Test*/
    var mockRepository = new MockRepository(); /*mock repository has public methods to check state*/
    var myMap = new Map(mockRepository);
    myMap.put(/*whatever test input*/);
    /* here use the mock repository to check that mutation of state occurred as expected based on ititial state of respository and input */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.