# Class goog.testing.PropertyReplacer

code »

Helper class for stubbing out variables and object properties for unit tests. This class can change the value of some variables before running the test cases, and to reset them in the tearDown phase. See googletest.StubOutForTesting as an analogy in Python: http://protobuf.googlecode.com/svn/trunk/python/stubout.py Example usage:

    var stubs = new goog.testing.PropertyReplacer();

     function setUp() {
       // Mock functions used in all test cases.
       stubs.set(Math, 'random', function() {
         return 4;  // Chosen by fair dice roll. Guaranteed to be random.
       });
     }

     function tearDown() {
       stubs.reset();
     }

     function testThreeDice() {
       // Mock a constant used only in this test case.
       stubs.set(goog.global, 'DICE_COUNT', 3);
       assertEquals(12, rollAllDice());
     }

Constraints on altered objects:

- DOM subclasses aren't supported.
- The value of the objects' constructor property must either be equal to the real constructor or kept untouched.

## Constructor

goog.testing.PropertyReplacer ( )
Show:Public Protected Private

## Instance Methods

code »remove ( obj, key )

Deletes the key from the object while saving its original value.

Parameters

obj: `(Object|Function)`
  The JavaScript or native object or function to alter. See the constraints in the class description.
key: `string`
  The key to delete.

code »replace ( obj, key, value )

Changes an existing value in an object to another one of the same type while saving its original state. The advantage of `replace` over `#set` is that `replace` protects against typos and erroneously passing tests after some members have been renamed during a refactoring.

Parameters

obj: `(Object|Function)`
  The JavaScript or native object or function to alter. See the constraints in the class description.
key: `string`
  The key to change the value for. It has to be present either in `obj` or in its prototype chain.
value: `*`
  The new value to set. It has to have the same type as the original value. The types are compared with `goog.typeOf`.

Throws

`Error`
  In case of missing key or type mismatch.

code »reset ( )

Resets all changes made by goog.testing.PropertyReplacer.prototype.set.

code »set ( obj, key, value )

Adds or changes a value in an object while saving its original state.

Parameters

obj: `(Object|Function)`
  The JavaScript or native object or function to alter. See the constraints in the class description.
key: `string`
  The key to change the value for.
value: `*`
  The new value to set.

code »setPath ( path, value )

Builds an object structure for the provided namespace path. Doesn't overwrite those prefixes of the path that are already objects or functions.

Parameters

path: `string`
  The path to create or alter, e.g. 'goog.ui.Menu'.
value: `*`
  The value to set.

## Instance Properties

code »original_ : `Array.<Object>`

Stores the values changed by the set() method in chronological order. Its items are objects with 3 fields: 'object', 'key', 'value'. The original value for the given key in the given object is stored under the 'value' key.

## Static Functions

code »goog.testing.PropertyReplacer.deleteKey_ ( obj, key )

Deletes a key from an object. Sets it to undefined or empty string if the delete failed.

Parameters

obj: `(Object|Function)`
  The object or function to delete a key from.
key: `string`
  The key to delete.

code »goog.testing.PropertyReplacer.hasKey_ ( obj, key ) ⇒ `boolean`

Tells if the given key exists in the object. Ignores inherited fields.

Parameters

obj: `(Object|Function)`
  The JavaScript or native object or function whose key is to be checked.
key: `string`
  The key to check.

Returns

Whether the object has the key as own key.

## Static Properties

code »goog.testing.PropertyReplacer.NO_SUCH_KEY_ : `Object`

Indicates that a key didn't exist before having been set by the set() method.