-
-
Notifications
You must be signed in to change notification settings - Fork 775
Issue 1598 (Feature Request): sandbox.createStubInstance #1602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mroderick
merged 1 commit into
sinonjs:master
from
dpmott:issue-1598-implement-sandbox.createStubInstance
Nov 2, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ var sinonCollection = require("../lib/sinon/collection"); | |
| var sinonSpy = require("../lib/sinon/spy"); | ||
| var sinonStub = require("../lib/sinon/stub"); | ||
| var assert = referee.assert; | ||
| var refute = referee.refute; | ||
| var deprecated = require("../lib/sinon/util/core/deprecated"); | ||
|
|
||
| describe("collection", function () { | ||
|
|
@@ -18,6 +19,96 @@ describe("collection", function () { | |
| assert.isFunction(collection.mock); | ||
| }); | ||
|
|
||
| describe(".createStubInstance", function () { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests have largely been copied from stub-test.js. |
||
| beforeEach(function () { | ||
| this.collection = Object.create(sinonCollection); | ||
| }); | ||
|
|
||
| it("stubs existing methods", function () { | ||
| var Class = function () {}; | ||
| Class.prototype.method = function () {}; | ||
|
|
||
| var stub = this.collection.createStubInstance(Class); | ||
| stub.method.returns(3); | ||
| assert.equals(3, stub.method()); | ||
| }); | ||
|
|
||
| it("resets all stub methods on reset()", function () { | ||
| var Class = function () {}; | ||
| Class.prototype.method1 = function () {}; | ||
| Class.prototype.method2 = function () {}; | ||
| Class.prototype.method3 = function () {}; | ||
|
|
||
| var stub = this.collection.createStubInstance(Class); | ||
| stub.method1.returns(1); | ||
| stub.method2.returns(2); | ||
| stub.method3.returns(3); | ||
|
|
||
| assert.equals(3, stub.method3()); | ||
|
|
||
| this.collection.reset(); | ||
| assert.equals(undefined, stub.method1()); | ||
| assert.equals(undefined, stub.method2()); | ||
| assert.equals(undefined, stub.method3()); | ||
| }); | ||
|
|
||
| it("doesn't stub fake methods", function () { | ||
| var Class = function () {}; | ||
|
|
||
| var stub = this.collection.createStubInstance(Class); | ||
| assert.exception(function () { | ||
| stub.method.returns(3); | ||
| }); | ||
| }); | ||
|
|
||
| it("doesn't call the constructor", function () { | ||
| var Class = function (a, b) { | ||
| var c = a + b; | ||
| throw c; | ||
| }; | ||
| Class.prototype.method = function () {}; | ||
|
|
||
| var stub = this.collection.createStubInstance(Class); | ||
| refute.exception(function () { | ||
| stub.method(3); | ||
| }); | ||
| }); | ||
|
|
||
| it("retains non function values", function () { | ||
| var TYPE = "some-value"; | ||
| var Class = function () {}; | ||
| Class.prototype.type = TYPE; | ||
|
|
||
| var stub = this.collection.createStubInstance(Class); | ||
| assert.equals(TYPE, stub.type); | ||
| }); | ||
|
|
||
| it("has no side effects on the prototype", function () { | ||
| var proto = { | ||
| method: function () { | ||
| throw "error"; | ||
| } | ||
| }; | ||
| var Class = function () {}; | ||
| Class.prototype = proto; | ||
|
|
||
| var stub = this.collection.createStubInstance(Class); | ||
| refute.exception(stub.method); | ||
| assert.exception(proto.method); | ||
| }); | ||
|
|
||
| it("throws exception for non function params", function () { | ||
| var types = [{}, 3, "hi!"]; | ||
|
|
||
| for (var i = 0; i < types.length; i++) { | ||
| // yes, it's silly to create functions in a loop, it's also a test | ||
| assert.exception(function () { // eslint-disable-line no-loop-func | ||
| this.collection.createStubInstance(types[i]); | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe(".stub", function () { | ||
| beforeEach(function () { | ||
| this.collection = Object.create(sinonCollection); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was necessary to run
npm buildornpm testlocally on a Windows box. If it's inappropriate, then I'll happily revert this change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine. At some point we agreed that it was not a pre-requisite that the entire build process runs on Windows, as none of the maintainers were using it, but we are certainly not opposed to improving the situation :-)