Creating Factories
Factories provide an easy way of generating all kinds of entities with different states. The goal of Factories is to make it easier to write and maintain tests that have the need for fixtures.
Creating the Factory Builder
The FactoryBuilder is the entry point for all interactions with factories that you create. It is the place where you store your factories so that they can be used throughout your test suite. New factories are created with a state of default
. Other states can be added after the initial creation.
1import { FactoryBuilder } from "@packages/core-test-framework";2 3const builder: FactoryBuilder = new FactoryBuilder();
Creating a Factory
A Factory is responsible for creating a random entity inside of your tests. Those entities will be random by default but you have the ability to modify them or alter the way they are generated completely.
1import { Identities } from "@arkecosystem/crypto"; 2import { generateMnemonic } from "bip39"; 3 4builder.set("Wallet", () => { 5 const passphrase: string = generateMnemonic(); 6 7 const { publicKey, privateKey } = Identities.Keys.fromPassphrase(passphrase); 8 9 return {10 publicKey,11 privateKey,12 address: Identities.Address.fromPassphrase(passphrase),13 wif: Identities.WIF.fromPassphrase(passphrase),14 passphrase,15 };16});
Creating entities with a deterministic outcome
Sometimes you might have the need for an entity that always returns the same output. You can achieve this by respecting the options
argument that is passed in, which could contain a passphrase for example.
1import { Identities } from "@arkecosystem/crypto"; 2 3builder.set("Wallet", (previous, options) => { 4 const passphrase: string = options.passphrase; 5 6 const { publicKey, privateKey } = Identities.Keys.fromPassphrase(passphrase); 7 8 return { 9 publicKey,10 privateKey,11 address: Identities.Address.fromPassphrase(passphrase),12 wif: Identities.WIF.fromPassphrase(passphrase),13 passphrase,14 };15});
Altering an entity after creation
If you need to perform any tasks after an entity has been created, you can use the afterMaking
method and pass in a method that should be called. The method will receive a single argument which is the newly generated entity after all modifiers have been applied to it.
1builder2 .get("Wallet")3 .afterMaking(entity => (entity.hooked = true));
Creating a Factory State
In certain scenarios you might need multiple versions of the same entity with slight differences. States can be used to achieve this by adding additional or altering the existing data.
1import { generateMnemonic } from "bip39";2 3builder.get("Wallet").state("secondPassphrase", () => {4 return {5 secondPassphrase: generateMnemonic(),6 };7});
Altering an entity after state creation
If you need to perform any tasks after an entity with a specific state has been created, you can use the afterMakingState
method and pass in a method that should be called. The method will receive a single argument which is the newly generated entity after all modifiers have been applied to it.
1builder2 .get("Wallet")3 .afterMakingState("secondPassphrase", entity => (entity.hooked = true));
Generating an Entity through a Factory
After you’ve created and registered your factory, you can go ahead and generate an entity with the following snippet. It will create a single entity that, if you need more you can pass in any number as the first argument and it will generate as many entities as specified, applying all modifiers on the way.
1builder.get("Wallet").make();