8000 feat: added a section on mocking IoC bindings by realprabs · Pull Request #199 · adonisjs/v5-docs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

feat: added a section on mocking IoC bindings #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions content/guides/testing/fakes.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,28 @@ test('transfer payment', async ({ client }) => {
})
```

## Mocking with IoC container
IoC container allows you to replace the registered bindings with the fake ones while testing using `.fake` method. You can enable it by adding `.useProxies()` in `test.ts` before the kernel is booted.

```ts
// test.ts
const kernel = new Ignitor(__dirname).kernel('test');
kernel.application.container.useProxies();

kernel.boot()
```

You can now mock any service registered in the IoC container in your tests.

```ts
// some.spec.ts
import Application from '@ioc:Adonis/Core/Application';

test('service call', ({ expect }) => {
Application.container.fake('My/Service', () => mock);
})
```

## Mocking network requests
You can use [nock](https://github.com/nock/nock) to mock the outgoing network requests. Since nock works by overriding the Node.js `http.request`, it works with almost every HTTP client, including `axios` and `got`.

Expand Down
0