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

Backend not being intercepted #27

Closed RyanWarner closed 9 years ago

RyanWarner commented 9 years ago

First of all, thank you for such an intuitively designed tool. I foresee this being incredibly useful and a joy to use.

I am unable to get my backend mocked. I have followed the setup instructions, studied the example, and browsed closed issues all with no luck.

Can you point me in any direction to debug this? Would pasting some of my config be useful? If there is any more information needed to diagnose the issue please let me know.

Thank you for your time.

atecarlos commented 9 years ago

Hi @RyanWarner . Thanks for that. Glad you are finding it useful. Could you post a sample test here? We can start there and see what we can do to fix your problem.

RyanWarner commented 9 years ago

Thanks for the help. Here is my example test. GET /api/user/list is called when the controller loads. POST /api/user/list is called when clicking the .enter-icon.

When I run my server locally, I can see it (correctly) logging "Unauthorized".

'use strict';

var mock = require( 'protractor-http-mock' );

describe( 'App List', function(  )
{
    afterEach( function(  )
    {
        mock.teardown(  );
    } );

    it( 'should add an item to the list', function(  )
    {
        mock(
        [
            {
                request:
                {
                    path: '/api/user/list',
                    method: 'POST'
                },
                response:
                {
                    status: 200,
                    data:
                    {
                        newListItem:
                        {
                            _id: 'abc',
                            name: 'new list item'
                        }
                    }
                }
            },
            {
                request:
                {
                    path: '/api/user/list',
                    method: 'GET'
                },
                response:
                {
                    status: 200,
                    data: []
                }
            }
        ] );

        browser.get( 'http://localhost:8080/app/list' );

        element( by.model( 'newItem' ) ).sendKeys( 'new list item' );
        element( by.css( '.enter-icon' ) ).click(  );

        var list = element.all( by.repeater( 'item in list' ) );

        expect( list.count(  ) ).toEqual( 1 );
        expect( list.get( 0 ).element( by.model( 'item.name' ) ).getAttribute( 'value' ) ).toEqual( 'new list item' );

        // expect( mock.requestsMade(  ) ).toEqual(
        // [
        //  { url: '/api/user/list', method: 'POST' },
        //  { url: '/api/user/list', method: 'GET' }
        // ] );
    } );
} );
RyanWarner commented 9 years ago

Here is my Protractor config file:

exports.config =
{
    seleniumServerJar: '../node_modules/selenium-standalone-jar/bin/selenium-server-standalone-2.45.0.jar',
    capabilities:
    {
        'browserName': 'chrome'
    },
    baseUrl: 'http://rywar.local:9000',
    onPrepare: function(  )
    {
        require( 'protractor-http-mock' ).config =
        {
            rootDirectory: __dirname,
            protractorConfig: 'protractor.config.js'
        }
    }
};
RyanWarner commented 9 years ago

In case it is more helpful to have access to everything, here is the repository and branch I'm working in: https://github.com/RyanWarner/app-sprout-client/tree/e2e-tests

That being said I have no problem pasting snippets. Whatever is easiest for you.

atecarlos commented 9 years ago

Hi @RyanWarner . At first glance, your GET mock object is defining a data property with an empty array which I assume is not actually passed in when you make the call. Therefore, when its matching the requests, it's trying to match an empty data property with a non existing data property, and therefore its not mocking your call.

Could you try your test removing that?

RyanWarner commented 9 years ago

That's in the response though. It shouldn't really matter what data is in the response should it? I'm intentionally returning an empty array here. The user has just signed up, and their list is empty.

Regardless, I tried removing it and it had no effect.

atecarlos commented 9 years ago

Hi @RyanWarner . Yeah you are completely correct. My mistake. I looked at it too early in the morning, and must have read it wrong. From what you put in I can't really see anything wrong with what you are doing.

If you do browser.pause() after your page is loaded, do you see any errors in your console on the browser?

If no, we can try opening up the source code (httpMock.js) and putting some console.log statements there to see if it's actually getting called and what exactly is coming in to the $http method.

Let's start with that and see where that leads us.

Akenaide commented 9 years ago

Hello,

You can try to put "post" in lowercase. I had a similarly issue and it was solved by putting "get" in lowercase in the mock's requests.

enigma1 commented 9 years ago

In protractor-http-mock/lib/http-mock.js The function match was changed from 0.1.8 to 0.1.9 and 0.1.10 and from what I tested its the reason tests now are failing because of the expectation mismatch

The old function was like this

function match(config, expectationRequest){
        return expectationRequest.method === config.method &&
            endsWith(config.url, expectationRequest.path) &&
            (!expectationRequest.params || objectMatch(expectationRequest.params, config.params)) &&
            (!expectationRequest.data || objectMatch(expectationRequest.data, config.data));
      }

Notice the "OR" operators for the params and data arguments. With the latest versions these are no longer optional but I am not sure why.. I went back to version 0.1.8 to get around this problem.

atecarlos commented 9 years ago

Hi @enigma1 , in my own testing I didn't run through any problems with the recent changes. I would be glad to take a look, can you create a new issue and provide some samples tests?

atecarlos commented 9 years ago

Hi @Akenaide , I agree that the casing should not matter and I'll add some code in to ignore the case in a new version.

@RyanWarner , did this suggestion solve your problem by any chance?

atecarlos commented 9 years ago

Hi @Akenaide . I just published version 0.1.11 with a fix for the casing issues. Please let me know if this solves your problem permanently.

RyanWarner commented 9 years ago

I'm sorry I have been busy apartment hunting and had to put this down for a few days. Will take a look soon.

RyanWarner commented 9 years ago

Hey @Akenaide,

I updated to the latest version (0.1.12) and it seems to be working. Thank you for the fast fix.

I did notice something when trying to get it to work... see #30