Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Sep 25, 2024. It is now read-only.
/stand-inPublic archive

Method replacement and testing utility

License

NotificationsYou must be signed in to change notification settings

cjihrig/stand-in

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Method replacement and testing utility

Build Statusnpm versionDependenciesbelly-button-style

Description

Often when unit testing, it is helpful to capture or replace function calls with testing code.stand-in provides a quick and easy way to replace object methods with your own. It also provides a method to restore the original method after testing is complete.

Usage

varstandin=require('stand-in');varassert=require('assert');varlog=standin.replace(console,'log',function(stand,value){assert.strictEqual(value,'test data','value should equal test data');stand.restore();});console.log('test data');
  • replace(obj, path, fn [, options]) - replacesobj[path] withfn where:
    • obj - object that has the method to replace. Will be used atthis pointer insidefn.
    • path - string path to the function to replace. Supports deep paths via "foo.bar.baz".
    • fn - function to replaceobj[method] with. The first argument to this function will be a stand-in object. This is helpful if you don't want to create a holding variable.
    • options - an optional object supporting the following properties.
      • startOn - the invocation number to begin using the replacement function. Defaults to0.
      • stopAfter - the final invocation number to use the replacement function. Once this number is reached, thestand-in object willrestore() itself. Defaults toInfinity.

Returns astand-in object:

  • restore() - restores the originalobj[method] to the previous function. Generally, this will restore the method back to the initial value.
  • original - a handle to the original method in case you need to conditionally call it.
  • invocations - the number of times thestand-in has been called.

replaceOnce() can be used to create a one time replacement. It is identical toreplace(), except thestopAfter option will be set automatically to an appropriate value, depending on the value ofstartOn.

Note

replace tries to prevent users from completely losing a handle to the original method. For example, you will receive anAssertionError if you try to replaceconsole.log twice without firstrestoreing the function first.


[8]ページ先頭

©2009-2025 Movatter.jp