# createTestCanister

> **createTestCanister**\<`A`\>(`idlFactory`, `handlers`): [`FakeCanister`](https://ic-reactor.b3pay.net/v3/libs/interfaces/fakecanister/)

Defined in: [core/src/testing/test-canister.ts:101](https://github.com/B3Pay/ic-reactor/blob/f1956947ae037304fce1675a38695964c1aa9e32/packages/core/src/testing/test-canister.ts#L101)

Builds a canister for [installFakeReplica](https://ic-reactor.b3pay.net/v3/libs/functions/installfakereplica/) from the service's Candid
interface and a handler per method, typed from the service. The fake
replica decodes each call's arguments with the interface, runs the
method's handler and encodes its result, so the reactor under test encodes,
decodes, caches and unwraps exactly as it does against a replica.

A query call to a method the interface does not mark `query` or
`composite_query` is rejected, as a replica rejects it. A method with no
handler rejects every call to it, and a handler for a method the service
does not have throws here.

Return `{ Err: ... }` from a method that returns a `Result` to test the
`CanisterError` a reactor throws for it, and throw from a handler to test
the `CallError` a trap produces.

## Type Parameters

### A

`A` = [`BaseActor`](https://ic-reactor.b3pay.net/v3/libs/type-aliases/baseactor/)

The service type, such as the `_SERVICE` generated with the
`idlFactory`. It types each handler's arguments and result.

## Parameters

### idlFactory

(`IDL`) => `any`

The service's Candid interface, as a reactor takes it.

### handlers

[`TestCanisterHandlers`](https://ic-reactor.b3pay.net/v3/libs/type-aliases/testcanisterhandlers/)\<`A`\>

The methods the test canister answers.

## Returns

[`FakeCanister`](https://ic-reactor.b3pay.net/v3/libs/interfaces/fakecanister/)

A canister to install under its ID in `installFakeReplica`.

## Example

```typescript
import { createTestCanister, installFakeReplica } from "@ic-reactor/core/testing"
import { idlFactory, type _SERVICE } from "./declarations/backend"

const balances = new Map<string, bigint>()

const replica = installFakeReplica({
  canisters: {
    "bkyz2-fmaaa-aaaaa-qaaaq-cai": createTestCanister<_SERVICE>(idlFactory, {
      // The caller is the principal the agent signed the call as.
      balance: (_args, { caller }) => balances.get(caller.toText()) ?? 0n,
      deposit: ([amount], { caller }) => {
        if (amount === 0n) return { Err: { InvalidAmount: null } }
        const next = (balances.get(caller.toText()) ?? 0n) + amount
        balances.set(caller.toText(), next)
        return { Ok: next }
      },
    }),
  },
})
```