mdasberg / ng-apimock

Node plugin that provides the ability to use scenario based api mocking: for local development for protractor testing
MIT License
99 stars 26 forks source link

selectScenario is not selecting the requested scenario #40

Closed Local9 closed 6 years ago

Local9 commented 6 years ago

I'm calling ngApimock.selectScenario('welcome', 'welcome-new-title'); correctly in my step but it is only acting as a pass through unless default is defined true, to test this I added another scenario. Am I getting something wrong?

Mock file: welcome.json

{
  "expression": "api\/v1\/welcome",
  "method": "GET",
  "name": "welcome",
  "isArray": "false",
  "responses": {
    "welcome-title": {
      "default": true,
      "status": 200,
      "headers": {},
      "data": { "message": "Welcome to app!!" }
    },
    "welcome-new-title": {
      "default": false,
      "status": 200,
      "headers": {},
      "data": { "message": "Welcome to the new app!!" }
    }
  }
}

service http request:

getWelcome() {
    return this.http.get('api/v1/welcome').map(response => response.json())
    .catch((error: Response | any) => {
      return Observable.throw(error.json());
    }).toPromise();
  }

Edit: I'm being returned "Cannot GET /api/v1/welcome" on the mocking page

Edit: Updated expression to regex match (regex101.com), still same issue.

mdasberg commented 6 years ago

I've tried to reproduce your situation, but when I use your mock and I call the url I get the correct answer.

When I call the url without changing the scenario I get

curl http://localhost:9900/api/v1/welcome

results in

{"message": "Welcome to app!!"}

When I call the url after selecting the scenario "welcome-new-title" I get this

curl http://localhost:9900/api/v1/welcome

results in

{"message": "Welcome to the new app!!"}

can you show me your setup?

Local9 commented 6 years ago

My setup is here: https://github.com/Local9/AngularBDD-serenityJS

mdasberg commented 6 years ago

I just checked out your repo and checked the code.

in your mock file you have both scenarios set to false which results in a passThrough.

when you call ngApimock.selectScenario in your po file like this:

navigateTo() {
    return ngApimock.selectScenario('welcome', 'welcome-title').then(()=> {
      return browser.get('/');
    })
  }

your tests is succesful

Local9 commented 6 years ago

Ok so selectScenario returns a promise which will then make it return the correct scenario? Ah, I think I see it now.

mdasberg commented 6 years ago

yes every method on ngApimock returns a promise.

Local9 commented 6 years ago

Sorry to re-open this, same issue still remains using selectScenario, am I reading this wrong?!

selectScenario(name, scenarioName, options)

Selects the given scenario (when calling this function without a scenario or with 'passThrough' as scenario name, the call will be passed through to the actual backend)

I am declaring which scenario I want it to use but it is not, I can see it making the calls to the API layer when running my tests but the scenario is not correctly selected.

Process;

BDD Testing with Cucumber;

Neither change from the default scenario. So am I reading your method wrong in that I should be allowed to change the scenario my tests run in my tests or is this not the case?

I have updated as per your recommendations and can be seen in the repo.

Local9 commented 5 years ago

So, spent the day back on this after doing a bunch of updates to my repo... still doesn't work.

If I set the mock on the mocking page, works as expected. So selectScenario method is not working as I expected. I can see the PUT, but the GET returns default and not the expected scenario, from my understanding this should set the scenario, just like the drop down on the mocking page. I get that this is working fine for most other cases, I just would love for this to work in this repo as I have not yet got this to work.

As to what you said about both my mocks having default set to false, after posting this I updated my repo to show the selectScenario is not working, I have inturn updated this post to show that this is the case.

Console Output;

C:\Users\acook\workspace\ng2\AngularBDD-serenityJS>ng e2e
****************************************************************************************
This is a simple server for use in testing or debugging Angular applications locally.
It hasn't been reviewed for security issues.

DON'T USE IT FOR PRODUCTION!
****************************************************************************************
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
 10% building modules 3/3 modules 0 active[HPM] Proxy created: /api  ->  http://localhost:3000
[HPM] Subscribed to http-proxy events:  [ 'error', 'close' ]
[HPM] Proxy created: /ngapimock  ->  http://localhost:3000
[HPM] Subscribed to http-proxy events:  [ 'error', 'close' ]

Date: 2019-01-12T12:50:19.956Z
Hash: dadd9efa9550cf70dc4e
Time: 9140ms
chunk {main} main.js, main.js.map (main) 14.5 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 259 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.08 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 16.3 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 3.97 MB [initial] [rendered]
i ?wdm?: Compiled successfully.
[12:50:20] I/update - chromedriver: file exists C:\Users\acook\workspace\ng2\AngularBDD-serenityJS\node_modules\protractor\node_modules\webdriver-manager\selenium\chromedriver_2.45.zip
[12:50:20] I/update - chromedriver: unzipping chromedriver_2.45.zip
[12:50:20] I/update - chromedriver: chromedriver_2.45.exe up to date
C:\Users\acook\workspace\ng2\AngularBDD-serenityJS
[12:50:21] I/launcher - Running 1 instances of WebDriver
[12:50:21] I/direct - Using ChromeDriver directly...

DevTools listening on ws://127.0.0.1:63639/devtools/browser/cc4913e7-0ba4-4373-85f5-80dbae2bc92c
.[HPM] PUT /ngapimock/mocks -> http://localhost:3000
[HPM] GET /api/v1/welcome -> http://localhost:3000
.F.

Failures:

1) Scenario: I open the homepage and see a welcome message # e2e\features\welcome\welcome.feature:5
   V Before # e2e\steps\welcome\welcome_check.steps.ts:11
   V Given I am on the homepage # e2e\steps\welcome\welcome_check.steps.ts:15
   × Then I should see welcome message # e2e\steps\welcome\welcome_check.steps.ts:18
       AssertionError
           + expected - actual

           -This is the wrong title, yet is default.
           +Welcome to the app!!

           at C:\Users\acook\workspace\ng2\AngularBDD-serenityJS\e2e\steps\welcome\welcome_check.steps.ts:20:18
           at elementArrayFinder_.then (C:\Users\acook\workspace\ng2\AngularBDD-serenityJS\node_modules\protractor\built\element.js:804:32)
           at ManagedPromise.invokeCallback_ (C:\Users\acook\workspace\ng2\AngularBDD-serenityJS\node_modules\selenium-webdriver\lib\promise.js:1376:14)
           at TaskQueue.execute_ (C:\Users\acook\workspace\ng2\AngularBDD-serenityJS\node_modules\selenium-webdriver\lib\promise.js:3084:14)
           at TaskQueue.executeNext_ (C:\Users\acook\workspace\ng2\AngularBDD-serenityJS\node_modules\selenium-webdriver\lib\promise.js:3067:27)
           at asyncRun (C:\Users\acook\workspace\ng2\AngularBDD-serenityJS\node_modules\selenium-webdriver\lib\promise.js:2927:27)
           at C:\Users\acook\workspace\ng2\AngularBDD-serenityJS\node_modules\selenium-webdriver\lib\promise.js:668:7
           at <anonymous>
           at process._tickCallback (internal/process/next_tick.js:118:7)
   V After # node_modules\protractor-cucumber-framework\lib\resultsCapturer.js:26

1 scenario (1 failed)
2 steps (1 failed, 1 passed)
0m00.861s
[12:50:24] I/launcher - 0 instance(s) of WebDriver still running
[12:50:24] I/launcher - chrome #01 failed 1 test(s)
[12:50:24] I/launcher - overall: 1 failed spec(s)
[12:50:24] E/launcher - Process exited with error code 1
An unexpected error occurred: undefined

C:\Users\acook\workspace\ng2\AngularBDD-serenityJS>

welcome.po.ts; https://github.com/Local9/AngularBDD-serenityJS/blob/master/e2e/steps/welcome/welcome.po.ts

The response recorded;

{
  "mocks": [
    {
      "expression": "api/v1/welcome",
      "method": "GET",
      "name": "welcome",
      "isArray": "false",
      "responses": {
        "welcome-title": {
          "status": 200,
          "data": {
            "message": "This is the wrong title, yet is default."
          },
          "default": true
        },
        "real-welcome-title": {
          "status": 200,
          "data": {
            "message": "This is the title I want, but selectScenario is not setting it."
          },
          "default": false
        }
      },
      "identifier": "welcome"
    }
  ],
  "selections": {
    "welcome": "welcome-title"
  },
  "delays": {},
  "echos": {},
  "recordings": {
    "welcome": [
      {
        "data": "{\"message\":\"This is the wrong title, yet is default.\"}",
        "payload": "",
        "datetime": 1547297357356,
        "method": "GET",
        "url": "/api/v1/welcome",
        "statusCode": 200
      },
      {
        "data": "{\"message\":\"This is the wrong title, yet is default.\"}",
        "payload": "",
        "datetime": 1547297424677,
        "method": "GET",
        "url": "/api/v1/welcome",
        "statusCode": 200
      }
    ]
  },
  "record": true
}
mdasberg commented 5 years ago

@Local9 I will checkout the project and try to figure out what is going wrong.

Local9 commented 5 years ago

I swear its something simple. If you have any questions I'll answer them asap.