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

Protractor mock REST request #119

Closed gustavocoelho closed 7 years ago

gustavocoelho commented 7 years ago

I'm runing the following spec file from protractor-http-mock and trying to figure a way to add it on another project but it seams that i've been missing something because it doesn't pass the test neither issues poped up on the console indicating some directions

spec: https://github.com/atecarlos/protractor-http-mock/blob/master/example/spec/requestsMade.spec.js

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

describe('REST Request Content', function () {

beforeEach(function () {
    mock([{
        request: {
            path: '/actions',
            method: 'POST',
            regex: false, // Boolean to enable Regular Expression matching on path. This is an optional field.

            data: { // These match POST data. This is an optional field.
                detail: 'CHAVE'
            }

        },
        response: {
            data: { // This is the return value for the matched request
                actionName: 'LicensePlateEntered',
                actionValue: '62LZS7',
                message: 'Created. Action accepted for processing.'
            },
            status: 201 // The HTTP status code for the mocked response. This is an optional field.
        }
    }]);
});

afterEach(function(){
    mock.teardown();
});

it('should have REST POST request verified', function (){
    console.log("Testing...");

    console.log("mock.requestsMade().actionName: " +mock.requestsMade().actionName);

    expect(mock.requestsMade()).toEqual([
        {
            url : '/actions', 
            method : 'POST',
            /*                 
            data : { 
                actionName : 'LicensePlateEntered', 
                actionValue: '62LZS7', 
                message: 'Created. Action accepted for processing.'
            } 
            */
            data : { 
                detail: 'CHAVE'
            } 

         }            
    ]);
});

});

That's how the console is performing:

Suite: REST Request Content Testing... mock.requestsMade().actionName: undefined failed - should have REST POST request verified Suite failed: REST Request Conten

Any help would be appreciated

atecarlos commented 7 years ago

Hi @gustavocoelho , I think you are missing the part where you actually run the test. Just calling mock by itself doesn't do anything but setup the mocked data. But your app still needs to load and run. See the get function call on the example spec you are referencing.

gustavocoelho commented 7 years ago

Alright then, i've just switched to the following code and yet haven't fixed anyhow

var mock = require('protractor-http-mock'); var globalE2EFunctions = require('./functions/global.e2e.functions.js'); const SLEEP_TIMER = 1000;

describe('REST Request Content', function () {

beforeEach(function () {
    browser.get(''); //In protractor.conf.js there is a baseUrl pointing to 'http://localhost:3500', so there isn't need to indicate on the browser.get('')
    browser.actions().sendKeys(globalE2EFunctions.ANPR_SCREEN()).perform();
    mock([{
        response: {
            data: { // This is the return value for the matched request
                actionName: 'LicensePlateEntered',
                actionValue: '62LZS7',
                message: 'Created. Action accepted for processing.'
            },
            status: 201 // The HTTP status code for the mocked response. This is an optional field.
        },
        request: {
            path: '/actions',
            method: 'POST',
            data: { // These match POST data. This is an optional field.
                detail: 'CHAVE'
            }
        }
    }]);
});

afterEach(function(){
    mock.teardown();
});

it('should have REST POST request verified', function (){
    informPlate();
    console.log("Testing...");

    console.log("mock.requestsMade().actionName: " +mock.requestsMade().actionName);

    expect(mock.requestsMade()).toEqual([{
        url : '/actions', 
        method : 'POST',
        /*                 
        data : { 
            actionName : 'LicensePlateEntered', 
            actionValue: '62LZS7', 
            message: 'Created. Action accepted for processing.'
        } 
        */
        data : { 
            detail: 'CHAVE'
        } 
    }]);
});

});

informPlate = function() { console.log("Inform Plate function processing...");

var buttonKey;

buttonKey = browser.driver.findElement(by.id('button_A')); 
buttonKey.click();

browser.sleep(SLEEP_TIMER);

buttonKey = browser.driver.findElement(by.id('button_B'));
buttonKey.click();

browser.sleep(SLEEP_TIMER);

buttonKey = browser.driver.findElement(by.id('button_C'));
buttonKey.click();

browser.sleep(SLEEP_TIMER);

buttonKey = browser.driver.findElement(by.id('button_9'));
buttonKey.click();

browser.sleep(SLEEP_TIMER);

buttonKey = browser.driver.findElement(by.id('button_5'));
buttonKey.click();

browser.sleep(SLEEP_TIMER);

buttonKey = browser.driver.findElement(by.id('button_7'));
buttonKey.click();
browser.sleep(SLEEP_TIMER);

buttonKey = browser.driver.findElement(by.id('button_6'));
buttonKey.click();

var base_keyboard_ok = browser.driver.findElement(by.id('base-keyboard-ok'));
base_keyboard_ok.click();

browser.sleep(5000);

};

atecarlos commented 7 years ago

Try setting up the mock before you open the browser. That's an intentional order that you can see in the example specs.

gustavocoelho commented 7 years ago

Hi @atecarlos i've tryed by the end of the beforeEach method also right after it(...) instruction, none of them made any difference at all.

Protractor doesn't recognize mock.requestsMade().actionName: undefined

I haven't figure out what's wrong.

atecarlos commented 7 years ago

What does mock.requestsMade() return?

gustavocoelho commented 7 years ago

That's how is coming up on the console ManagedPromise::278 {[[PromiseStatus]]: "pending"}

atecarlos commented 7 years ago

Well that's expected, but what does the promise resolve to?

On Fri, Jun 9, 2017 at 11:16 AM, gustavocoelho notifications@github.com wrote:

That's how is coming up on the console ManagedPromise::278 {[[PromiseStatus]]: "pending"}

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/atecarlos/protractor-http-mock/issues/119#issuecomment-307417049, or mute the thread https://github.com/notifications/unsubscribe-auth/AAeEJdsc85Gwc3u4EUNxui1xCSgptMmXks5sCWG0gaJpZM4Nz9L6 .

-- Carlos Atencio

gustavocoelho commented 7 years ago

I suppose that matches data.detail attribute from the post request, alright?

mock([{
    response: {
        data: { 
            actionName: 'LicensePlateEntered',
            actionValue: '62LZS7',
            message: 'Created. Action accepted for processing.'
        },
        status: 201 
    },
    request: {
        path: '/actions',
        method: 'POST',
        data: { 
            detail: 'CHAVE'
        }
    }
}]);
expect(mock.requestsMade()).toEqual([{
    url : '/actions', 
    method : 'POST',
    data : { 
        detail: 'CHAVE'
    } 
}]);
atecarlos commented 7 years ago

Yes, that looks correct. Does that test pass?

gustavocoelho commented 7 years ago

No, it doesn't. Can't figure what's wrong.

atecarlos commented 7 years ago

At this point, I'm inclined to think that there must be something different happening in your app, and the request your app is doing is not what you are expecting it to. Unfortunately, that's not something I could help you with beyond pointing you to the example spec that you already saw for guidance on what to expect.

Good luck!