atecarlos / protractor-http-mock

A library designed to work alongside Protractor for setting up mocks for your HTTP ajax requests.
MIT License
173 stars 70 forks source link

Runtime mock matching doesn't seem to function #104

Closed jamessmithxyz closed 7 years ago

jamessmithxyz commented 8 years ago

Browser: Firefox Linux 30.0

Matching requests made and mocks doesn't seem to work because mock variables have no keys in matching. They are shown when printed with console.log but not present in compare. As a workaroud I redefine add and remove functions in protractor config file to fix the matching:

onPrepare: function(){ require('protractor-http-mock/index').remove = function(mocks){ return browser.executeAsyncScript(function () { angular.module('httpMock').removeMocks(JSON.parse(arguments[0])); var callback = arguments[arguments.length - 1]; callback(JSON.stringify(true)); }, JSON.stringify(mocks));
};

require('protractor-http-mock/index').add = function(mocks){ return browser.executeAsyncScript(function () { angular.module('httpMock').addMocks(JSON.parse(arguments[0])); var callback = arguments[arguments.length - 1]; callback(true); }, JSON.stringify(mocks));
};
}

This is my first issue here so I don't know how this process goes. It would be great if I could remove this workaround from my code. Until them use this workaround if this is not only my problem.

atecarlos commented 8 years ago

Hi @jamessmithxyz . I'm not quite clear on your problem. Could you provide more context?

jamessmithxyz commented 8 years ago

The problem is runtime mocks (Firefox Linux 30.0). In tests I'm setting mocks: mock = require 'protractor-http-mock/index' ... mock [my_mocks] ...do stuff that hits mocks mock.remove old_request mock.add new_request

This works when I use the presented workaround but not with the original implementation. In workaround I checked that browser.executeAsyncScript call secord argument (=mocks in original implementation) is not the same as within the first argument function. The second argument object has keys but the within the first argument function there are no keys. This is strange because with console.log the instances look the same. Instead of hash, I tried to use String value argument (stringify -> parse) and it solved the problem. I don't understard why this happened and is it somehow related to my environment. On the other hand, this stringify -> parse strategy here worked.

atecarlos commented 8 years ago

I'm not clear on what happens after

mock.remove old_request
mock.add new_request

Are you expecting the new_request to be used in "do stuff that hit mocks"?

jamessmithxyz commented 8 years ago

After these the application makes the same request that it did before changing the mocks. For example, 1) load and show of items, 2) click remove button, 3) load and show list of items. Check that the removed item is no longer shown. With the workaround the new mock is used. With the original code the new mock doesn't match with the request made although the objects are identical.

atecarlos commented 8 years ago

Hi @jamessmithxyz , have you been able to get these same tests to work on another browser? I'm seeing a trend here with issues using Firefox.