-
-
Notifications
You must be signed in to change notification settings - Fork 768
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
fixes issue #1368 by adding stub#resolvesThis #1517
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,6 +285,65 @@ describe("stub", function () { | |
}); | ||
}); | ||
|
||
describe(".resolvesThis", function () { | ||
afterEach(function () { | ||
if (Promise.resolve.restore) { | ||
Promise.resolve.restore(); | ||
} | ||
}); | ||
|
||
it("returns a promise resolves with this", function () { | ||
var instance = {}; | ||
instance.stub = createStub.create(); | ||
instance.stub.resolvesThis(); | ||
|
||
return instance.stub().then(function (actual) { | ||
assert.same(actual, instance); | ||
}); | ||
}); | ||
|
||
var strictMode = (function () { | ||
return this; | ||
}()) === undefined; | ||
if (strictMode) { | ||
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. I am not sure a conditional for strict mode is necessary:
If for some reason, someone decides to run the tests in non-strict mode or in engines that doesn't support that, then I think the tests should be allowed to fail. 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. Actually, the tests I wrote are very similar to those written for returnsThis. |
||
it("returns a promise resolves with undefined when detached", function () { | ||
var stub = createStub.create(); | ||
stub.resolvesThis(); | ||
|
||
// Due to strict mode, would be `global` otherwise | ||
return stub().then(function (actual) { | ||
assert.same(actual, undefined); | ||
}); | ||
}); | ||
} | ||
|
||
it("stub respects call", function () { | ||
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. Can this expectation be more specific, to make it easier to understand what "respects call" means? |
||
var stub = createStub.create(); | ||
stub.resolvesThis(); | ||
var object = {}; | ||
|
||
return stub.call(object).then(function (actual) { | ||
assert.same(actual, object); | ||
}); | ||
}); | ||
|
||
it("stub respects apply", function () { | ||
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. Can this expectation be more specific, to make it easier to understand what "respects apply" means? |
||
var stub = createStub.create(); | ||
stub.resolvesThis(); | ||
var object = {}; | ||
|
||
return stub.apply(object).then(function (actual) { | ||
assert.same(actual, object); | ||
}); | ||
}); | ||
|
||
it("returns stub", function () { | ||
var stub = createStub.create(); | ||
|
||
assert.same(stub.resolvesThis(), stub); | ||
}); | ||
}); | ||
|
||
describe(".returnsArg", function () { | ||
it("returns argument at specified index", function () { | ||
var stub = createStub.create(); | ||
|
@@ -2171,6 +2230,16 @@ describe("stub", function () { | |
refute.defined(instance.stub()); | ||
}); | ||
|
||
it("cleans 'resolvesThis' behavior", function () { | ||
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. It is generally better to be very specific when writing expectations, especially in codebases with distributed contributors. Generic expectations often lead to readers trying to derive the intent from the implementation of the test. If there's a mistake in the implementation, then the reader is likely to end up with an incorrect understanding of then intended behaviour. I would suggest rewriting the expectation to something like this: it("cleans 'resolvesThis' behavior using stub.resetBehavior", function () {
it("calls stub's resetBehavior method to clean behavior", function () { 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. I'll fix these expectations. Thanks for the suggestions. |
||
var instance = {}; | ||
instance.stub = createStub.create(); | ||
instance.stub.resolvesThis(); | ||
|
||
instance.stub.resetBehavior(); | ||
|
||
refute.defined(instance.stub()); | ||
}); | ||
|
||
describe("does not touch properties that are reset by 'reset'", function () { | ||
it(".calledOnce", function () { | ||
var stub = createStub(); | ||
|
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.
IMO we should keep this consistent with other behaviors, such as
rejects
, which also sets many other flags, as you can see here. Even though this is enough to make it work, I think that by keeping the whole API consistent when it comes to flagging fakes is extremely important.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.
I think you created the wrong link? It just points back to your comment.
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.
I guess I can set
returnValueDefined
andexception
flags to false to keep consistency. Do you think some other flags need to be turned off?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.
@fatso83 I think that link only works inside the files tab. When I click it inside of it the link works.
Basically I think the following flags should be set:
fake.returnValue
fake.returnValueDefined
fake.exception
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.
Okay now resolves, rejects and resolvesThis all act on the same flags.
I set returnValue to undefined as the stub resolves with itself.
Is it okay by you?