> For the complete documentation index, see [llms.txt](https://book.restfulnode.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.restfulnode.com/part-3/chapter-10/7-testing-the-controller.md).

# Testing the Controller

Recall how we implemented the `registerUser` controller.

*File: src/controllers/auth/registerUser.js*

```javascript
const catchExceptions = require('../../utils/catchExceptions')
const globalResponseDto = require('../../responses/globalResponseDto')
const userResponseDto = require('../../responses/userResponseDto')
const registerUserRequestDto = require('../../requests/registerUserRequestDto')
const registerUserValidator = require('../../validators/registerUserValidator')
const authService = require('../../domain/services/auth.service')
const EventEmitter = require('events')
const eventEmitter = new EventEmitter()

/**
 * Inserts the user into the database and fires off an email notification to that user's email if successful.
 */
const registerUser = catchExceptions(async (req, res) => {
  const registerUserRequest = registerUserRequestDto(req.body)

  registerUserValidator(registerUserRequest)

  const user = await authService.registerUser(registerUserRequest)

  eventEmitter.emit('userHasRegistered', user)

  res.json(
    globalResponseDto({
      status: 'success',
      code: 200,
      message: `The email: ${registerUserRequest.email} has successfully registered.`,
      data: userResponseDto(user),
      errors: null
    })
  )
})

module.exports = registerUser
```

### The Test

Now because our controller has been kept nice and thin up until this point and have barely any logical statements other than the functions and services we've created, there's actually very little to test. If you've done your job correctly, then you should not have to test the controller what so ever because all of your tests would have been done in those other layers.

However, since this is an educational book, we will do it for demonstration purposes.

In order to test the controller, the key is to mock every service that its using and see if they've been called or not.

Check out the following test case as we mock the `registerUserRequestDto`, the `registerUserValidator`, the `authService.register` function, and the express `req` and `res`.

*File: src/controllers/auth/\_\_tests\_\_/registerUser.test.js*

```javascript
const registerUserController = require('../registerUser')

const registerUserRequestDto = require('../../../requests/registerUserRequestDto')
const registerUserValidator = require('../../../validators/registerUserValidator')
const authService = require('../../../domain/services/auth.service')

jest.mock('../../../requests/registerUserRequestDto', () =>
  jest.fn((data) => data)
)
jest.mock('../../../validators/registerUserValidator', () =>
  jest.fn((data) => data)
)
jest.mock('../../../domain/services/auth.service', () => {
  return {
    registerUser: jest.fn((data) => data)
  }
})
const mockRequest = () => ({
  body: {
    first_name: 'john',
    last_name: 'doe'
  }
})

const mockResponse = () => {
  const res = {}

  res.status = jest.fn().mockReturnValue(res)
  res.json = jest.fn().mockReturnValue(res)

  return res
}

describe('Controler - Register User', () => {
  test('User should be registered successfully', async () => {
    const req = mockRequest()
    const res = mockResponse()

    await registerUserController(req, res)

    const randomUserFromBodyRequest = {
      first_name: 'john',
      last_name: 'doe'
    }

    expect(registerUserRequestDto).toHaveBeenCalledWith(
      randomUserFromBodyRequest
    )

    expect(registerUserValidator).toHaveBeenCalledWith(
      randomUserFromBodyRequest
    )

    expect(authService.registerUser).toHaveBeenCalledWith(
      randomUserFromBodyRequest
    )

    expect(res.status).toHaveBeenCalledWith(201)
    expect(res.json).toHaveBeenCalled()
  })
})
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.restfulnode.com/part-3/chapter-10/7-testing-the-controller.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
