{"action":"opened","issue":{"url":"https://api.github.com/repos/atecarlos/protractor-http-mock/issues/67","labels_url":"https://api.github.com/repos/atecarlos/protractor-http-mock/issues/67/labels{/name}","comments_url":"https://api.github.com/repos/atecarlos/protractor-http-mock/issues/67/comments","events_url":"https://api.github.com/repos/atecarlos/protractor-http-mock/issues/67/events","html_url":"https://github.com/atecarlos/protractor-http-mock/issues/67","id":128546302,"number":67,"title":"Using this module to load mocks into an app as an angular module, outside of protractor","user":{"login":"w-green","id":1765971,"avatar_url":"https://avatars.githubusercontent.com/u/1765971?v=3","gravatar_id":"","url":"https://api.github.com/users/w-green","html_url":"https://github.com/w-green","followers_url":"https://api.github.com/users/w-green/followers","following_url":"https://api.github.com/users/w-green/following{/other_user}","gists_url":"https://api.github.com/users/w-green/gists{/gist_id}","starred_url":"https://api.github.com/users/w-green/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/w-green/subscriptions","organizations_url":"https://api.github.com/users/w-green/orgs","repos_url":"https://api.github.com/users/w-green/repos","events_url":"https://api.github.com/users/w-green/events{/privacy}","received_events_url":"https://api.github.com/users/w-green/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-01-25T14:19:14Z","updated_at":"2016-01-25T14:19:14Z","closed_at":null,"body":"We have been using Protractor Http Mock, and we’re happy with it.\r\n\r\nHowever we saw some shortcomings… In particular the mocks will not be used if you refresh the page, or follow a click to a new page.\r\n\r\nThese both boil down to the use of adding the mocks via Protractor’s addMockModule.\r\n\r\nIdeally it would be good if we could load the mocks into the app, before we run the e2e’s. Then we could refresh the page and the same mocks would be used.\r\n\r\nI think the module could be broken down better for use in this manner, if this was something you were considering in the future.\r\n\r\n**I’ve outlined how we approached this problem ourselves, and wondered if there had been any similar thoughts and attempts?**\r\n\r\nOur ultimate goal was to be able to use mocks loaded into our app as the default mocks. Then use the Protractor Http Mock to override ones where we needed to.\r\n\r\nWe used a custom mock-module file which we referenced in the html.\r\nThen used grunt to copy your module and add the mocks (example below) to our custom module.\r\n\r\n\r\nPlease see the examples below of the grunt file and the mock-module file. Underneath is a brief summary.\r\n\r\nInside a grunt file \r\n\r\n* require /protractor-http-mock/lib/httpMock\r\n* execute the exported default function with an empty string for the expectations\r\n* turn this into a string\r\n* add the mocks via find and replace of ‘expectations =‘\r\n* Change the name of the module (so that we can use Protractor Http Mocks to override mocks when we need to)\r\n* Then add this string to the custom mock-module file mentioned before.\r\n\r\nInside the custom mock-module we immediately invoke the function and added it to the app. \r\n\r\n\r\n\r\nexample grunt file\r\n```javascript\r\n  var httpMock = require(__dirname\r\n      + '/../node_modules/protractor-http-mock/lib/httpMock’)\r\n   , grunt = require('grunt’);\r\n\r\n  var customModule = ’src/e2e/mock-module/mocks.js'\r\n    , customModuleContent\r\n    , dest = ‘dest/e2e/mock-module/mocks.js'\r\n    , httpMockOutput = httpMock('')\r\n    , httpMockOutputString = httpMockOutput.toString();\r\n\r\n  httpMockOutputString = httpMockOutputString\r\n    .replace('expectations = []', 'expectations = ' + '[' + getMocks() + ']');\r\n  httpMockOutputString = httpMockOutputString\r\n    .replace(/httpMock/g, 'mockHttp');\r\n\r\n  customModuleContent = customModule\r\n    .read()\r\n    .replace(/'<protractor-http-mock>'/, httpMockOutputString);\r\n\r\n  grunt.file.write(dest, customModuleContent);\r\n```\r\n\r\nexample mock-module/mock.js\r\n```javascript\r\n('<protractor-http-mock>')();\r\n\r\n(function addModuleToApp() {\r\n  var app = document.documentElement.getAttribute('ng-app');\r\n\r\n  angular.module(app).requires.push('mockHttp');\r\n})();\r\n```\r\n\r\n\r\n\r\n\r\n\r\n\r\n"}}