Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Unable to easily pass context to addMockModule #695

Closed
Dawil opened this issue Apr 10, 2014 · 3 comments
Closed

Unable to easily pass context to addMockModule #695

Dawil opened this issue Apr 10, 2014 · 3 comments

Comments

@Dawil
Copy link

Dawil commented Apr 10, 2014

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's whenGET(/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 to addMockModule 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 to addMockModule 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 by browser.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.

@juliemr juliemr added the docs label Apr 11, 2014
bmenant added a commit to bmenant/protractor that referenced this issue May 6, 2014
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
@bmenant
Copy link
Contributor

bmenant commented May 6, 2014

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 addMockModule method, relying on the WebDriver’s executeScript method. I noticed the Protractor code was already using that elsewhere as well, so… it seems safe.

Feel free to test the feature I committed on a forked branch: bmenant/protractor@e110e63

# In your mock, reference arguments[0]
myMockModule = ->
    angular.module 'myModule', []
        .value 'data', arguments[0]

# In your spec, provide context at will
it 'should be able to get context from spec', ->
    fakeData = { foo: 'bar' }
    browser.addMockModule 'myModule', myMockModule, fakeData

    browser.get 'index.html'

    # expect whatever you want…

@Dawil
Copy link
Author

Dawil commented May 6, 2014

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 addMockModule.

@Dawil
Copy link
Author

Dawil commented May 8, 2014

I took a look and it's all good to me. This solves the problem of not being able to write useful helper functions for addMockModule. I'm happy with closing this issue if/when #787 gets merged. Nice work, @bmenant.

bmenant added a commit to bmenant/protractor that referenced this issue May 19, 2014
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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants