-
-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Restore sandbox after each .replace test Otherwise a failed test may not restore it. * .define method for temporarily defining new properties during the tests * better comment * detailed exception messages * properly delete the property during the cleanup * Add .define to more places * Document .define * Fix test * Code review suggestions * prettier --write
- Loading branch information
Showing
4 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ title: Sandboxes - Sinon.JS | |
breadcrumb: sandbox | ||
--- | ||
|
||
Sandboxes removes the need to keep track of every fake created, which greatly simplifies cleanup. | ||
Sandboxes remove the need to keep track of every fake created, which greatly simplifies cleanup. | ||
|
||
```javascript | ||
var sandbox = require("sinon").createSandbox(); | ||
|
@@ -181,6 +181,40 @@ A convenience reference for [`sinon.assert`](./assertions) | |
|
||
_Since `[email protected]`_ | ||
|
||
#### `sandbox.define(object, property, value);` | ||
|
||
Defines the `property` on `object` with the value `value`. Attempts to define an already defined value cause an exception. | ||
|
||
`value` can be any value except `undefined`, including `spies`, `stubs` and `fakes`. | ||
|
||
```js | ||
var myObject = {}; | ||
|
||
sandbox.define(myObject, "myValue", function () { | ||
return "blackberry"; | ||
}); | ||
|
||
sandbox.define(myObject, "myMethod", function () { | ||
return "strawberry"; | ||
}); | ||
|
||
console.log(myObject.myValue); | ||
// blackberry | ||
|
||
console.log(myObject.myMethod()); | ||
// strawberry | ||
|
||
sandbox.restore(); | ||
|
||
console.log(myObject.myValue); | ||
// undefined | ||
|
||
console.log(myObject.myMethod); | ||
// undefined | ||
``` | ||
|
||
_Since `[email protected]`_ | ||
|
||
#### `sandbox.replace(object, property, replacement);` | ||
|
||
Replaces `property` on `object` with `replacement` argument. Attempts to replace an already replaced value cause an exception. Returns the `replacement`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters