-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Unable to easily pass context to addMockModule
#695
Comments
Allow Protractor’s 'addMockModule' method to pass context to its mocks, providing an argument to the script which overrides a module. Rely on the WebDriver’s 'executeScript' method. Closes angular#695
Thank you very much @Dawil, your review is super useful. I am building browser extensions relying on several “unpredictable” services (both external HTTP API and browser javascript API which I try to abstract through isolated Angular services). For such context, this feature is a must have. Indeed, without any difficulty, I was able to add a third parameter to the Protractor’s Feel free to test the feature I committed on a forked branch: bmenant/protractor@e110e63
|
That looks sensible and I'll be able to try it out within a couple of days. It's definitely better than the current work around that I've been using which involves converting my data into a json string and interpolating that into the string script that is executed by |
Allow Protractor’s 'addMockModule' method to pass context to its mocks, providing arguments to the script which overrides a module. Support any arguments provided in addition to the module’s name and script. Those arguments may be referenced from the script via the `arguments` object. Rely on the WebDriver’s 'executeScript' method. Closes angular#695
Request
I'd like to be able to pass a context to
addMockModule
. I wasn't sure were to post this (does protractor share a google group with angular?), so I thought here would do.Context
I've been using
addMockModule
to mock out the httpbackend, similar to how it was described in #125. I use $httpbackend'swhenGET(/matchingurl/).respond(...)
to fake responses from the server. Everything worked fine until I tried to write a helper function when I discovered that the function being passed toaddMockModule
is not evaluated in the context it is written in; it is converted to a string and evaluated instead.Work Around
Since it's merely evaluated as a string I am able to get around this by
JSON.stringify
-ing my state, concatenating it into the callback passed toaddMockModule
and having it work like that. The error messages are hardly helpful and it feels dirty but it definitely works and my code elsewhere is cleaner because of it.Possible Solution
When researching the problem I read @wilkerlucio's comment at the end of #125 which indicates that this would be possible due to
executeScript
, which is called bybrowser.get
on mocked modules, supporting these arguments.I couldn't find any documentation at all for executeScript, but if it does support context as additional arguments then this hopefully wouldn't be too difficult to add.Documentation for executeScript suggests this is possible.I don't know if this is a feature request, bug (being evaluated outside of it's defined context really ought to be documented) or documentation of possible work around for others, so feel free to interpret this however you'd like.
The text was updated successfully, but these errors were encountered: