-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
feat: throw when trying to use .value() on a method #2631
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -3781,6 +3781,29 @@ describe("stub", function () { | |
assert.equals(myFunc.prop, "rawString"); | ||
}); | ||
|
||
it("allows stubbing getters", function () { | ||
const y = { | ||
get foo() { | ||
return "bar"; | ||
}, | ||
}; | ||
refute.exception(function () { | ||
createStub(y, "foo").value("bar"); | ||
}); | ||
}); | ||
Comment on lines
+3784
to
+3793
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. The original issue (#2629) does not deal with this issue, so this seems more like we are documenting existing (non-intentional) behavior? Dan does talk about getters, but that is the conventional Java-bean like getters, which are methods. 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. If we change it like in #2631 (comment), then this test will obviously also need to be changed |
||
|
||
it("disallows stubbing non-accessor methods", function () { | ||
const x = { | ||
getFoo: function getFoo() { | ||
return "bar"; | ||
}, | ||
}; | ||
|
||
assert.exception(function () { | ||
createStub(x, "getFoo").value("baz"); | ||
}, "Error: getFoo is a function, not a getter or value. Use .returns() instead of .value()"); | ||
}); | ||
|
||
it("allows stubbing object props with configurable false", function () { | ||
const myObj = {}; | ||
Object.defineProperty(myObj, "prop", { | ||
|
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.
Are we supposed to allow setting
.value()
on a getter property? Seems erronous.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 wasn't sure either ... to me it seemed more reasonable to use
.returns()
rather than.value()
on a getter. I'm happy to change it to do thatThere 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 we should just do the simplest thing: prevent using
value()
on a method. So "${rootStub.propName} is a function and should not be overridden by .value(). Use .returns() when creating a function that returns a value
.There is little reason to add synthetic property getters and setters to the mix, as it just adds more noise/possible confusion, IMHO.