swagger-node icon indicating copy to clipboard operation
swagger-node copied to clipboard

How does a mock controller work?

Open zhouhao27 opened this issue 8 years ago • 4 comments

I have the following yaml snippet:

/test:
    x-swagger-router-controller: test
    get:
      description: Testing
      operationId: test
      responses:
        "200":
          description: Success
        default:
          description: Error

Controller test.js in mock folder:

'use strict';

module.exports = {
  test: test_function
};

function test_function(req, res) {
  res.json('Hi, this is a test');
}

But it doesn't work. Can anybody tell how the mock controller works? Thanks.

zhouhao27 avatar Oct 30 '15 03:10 zhouhao27

It works if I put test.js inside controllers folder. But I want it inside mocks folder. Maybe my understanding is wrong about the mock controller. Then please tell me what the mocks folder is for?

zhouhao27 avatar Oct 30 '15 03:10 zhouhao27

That's exactly what the mocks folder is for. Are you running in mock mode? What are you seeing? Can you please run with DEBUG=swagger* environment var set?

theganyo avatar Oct 30 '15 04:10 theganyo

You would probably need to run your api project with the -m parameter, to run it in mock mode. See this.

davidpelayo avatar Apr 15 '16 08:04 davidpelayo

old question but i'll reply

to run without mocking use ... swagger project start to run with mocking use ... swagger project start -m

when not mocking, swagger will use /api/controllers/[x-swagger-router-controller].js

when mocking, swagger will use:

/api/mocks/[x-swagger-router-controller].js if it exists, else it will fallback to using the response descriptions defined in swagger.yaml

danday74 avatar Mar 27 '18 13:03 danday74