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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:49:13+00:00 2026-05-25T20:49:13+00:00

I am trying to do some javascript test driven development, and would like to

  • 0

I am trying to do some javascript test driven development, and would like to replace a dao with a mock one.

Lets say I have an object myObject that has a private var dao that I would like to create a setter for:

var dao = new RealDao();

function funcUsesDao() {
    dao.doStuff();
}

Then in another file I have a handle to an instance of this object and want to add a setter so I can do the following:

var mockDao = {function doStuff() { /*mock code */}};
myObject.setDao(mockDao);
myObject.funcUsesDao(); should use mock dao

Environment specifics are this is node.js and the object I have a handle to is obtained by doing var myObject = require('../myObject');

  • 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-25T20:49:14+00:00Added an answer on May 25, 2026 at 8:49 pm

    You would use a constructor for the class, setting the dao to RealDao and then in the setDao you would set that dao to the new dao. Alternately in the constructor you would put a reference to dao, and on null, assign it to a new RealDao(). (at least, that’s been my understanding of how people generally assign new interfaces for testing .. via the constructor)

    //something like this
    function Dao(interface){
      this.dao = interface || new RealDao();
    }
    

    Otherwise, what you listed above is accurate. you just have to provide the setDao method as you indicated in your code.

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

Sidebar

Related Questions

I'm having some Javascript woes, lets say we have a constructor function test(element) {
I have some javascript that I'm trying to retool using jQuery to learn the
I'm trying to filter some XML in JavaScript using E4X and have some specific
I'm trying out capybara with cucumber to get some better javascript test coverage. I
I am trying to extend some Javascript in one of my pages and for
I'm currently trying to write some unit test on some javascript files. My first
I am trying to put a value within some javascript code like so: <?
I am trying to write some front end Javascript tests that can test drag
I'm trying to fix some JavaScript bugs. Firebug makes debugging these issues a lot
I'm trying to incorporate some JavaScript unit testing into my automated build process. Currently

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.