Skip to content

@gala-chain/testAPI


API > TestChaincodeStub

Class: TestChaincodeStub

Mock implementation of Hyperledger Fabric ChaincodeStub for unit testing.

Provides a complete testing environment that simulates blockchain state operations without requiring an actual Fabric network. Supports state management, composite keys, range queries, and identity mocking.

Contents

Example

// Create stub with initial state
const stub = new TestChaincodeStub(
  ["methodName", "arg1", "arg2"],
  { "key1": "value1" },
  {}
);

// Mock creator identity
stub.mockCreator("CuratorOrg", "client|admin");

// Test state operations
await stub.putState("newKey", Buffer.from("newValue"));
const value = await stub.getState("newKey");

Extends

  • ChaincodeStub

Constructors

new TestChaincodeStub(args, state, writes)

new TestChaincodeStub(args, state, writes): TestChaincodeStub

Creates a new TestChaincodeStub instance.

Parameters

args: string[]

Array of arguments simulating chaincode invocation parameters

state: undefined | Record\<string, string>

Initial blockchain state as key-value pairs

writes: undefined | Record\<string, string>

Object to track state writes for testing assertions

Overrides

ChaincodeStub.constructor

Source

chain-test/src/unit/TestChaincodeStub.ts:136


new TestChaincodeStub(client, channel_id, txId, chaincodeInput, signedProposal)

new TestChaincodeStub(client, channel_id, txId, chaincodeInput, signedProposal): ChaincodeStubClassType

Parameters

client: any

channel_id: any

txId: any

chaincodeInput: any

signedProposal: any

Overrides

ChaincodeStub.constructor

Source

chain-test/src/unit/TestChaincodeStub.ts:34

Properties

creator

creator: unknown

Inherited from

ChaincodeStub.creator

Source

chain-test/src/unit/TestChaincodeStub.ts:36


deleteState

deleteState: (key) => Promise\<void>

Deletes a key from the blockchain state. Removes from current state and marks as deleted in writes tracking.

Parameters

key: string

Overrides

ChaincodeStub.deleteState

Source

chain-test/src/unit/TestChaincodeStub.ts:226


getState

getState: (key) => Promise\<Uint8Array>

Retrieves a value from the blockchain state by key. Returns empty buffer if key doesn't exist.

Parameters

key: string

Overrides

ChaincodeStub.getState

Source

chain-test/src/unit/TestChaincodeStub.ts:237


getStateByPartialCompositeKey

getStateByPartialCompositeKey: (objectType, attributes) => FabricIterable\<KV>

Retrieves state entries that match a partial composite key. Used for querying related objects by their composite key prefix.

Parameters

objectType: string

attributes: string[]

Overrides

ChaincodeStub.getStateByPartialCompositeKey

Source

chain-test/src/unit/TestChaincodeStub.ts:246


putState

putState: (key, value) => Promise\<void>

Stores a key-value pair in the blockchain state. Updates both the current state and writes tracking.

Parameters

key: string

value: Uint8Array

Overrides

ChaincodeStub.putState

Source

chain-test/src/unit/TestChaincodeStub.ts:214


state

readonly state: Record\<string, string>

Source

chain-test/src/unit/TestChaincodeStub.ts:126


writes

readonly writes: Record\<string, string>

Source

chain-test/src/unit/TestChaincodeStub.ts:127


creator

static creator: unknown

Inherited from

ChaincodeStub.creator

Source

chain-test/src/unit/TestChaincodeStub.ts:36


epoch

static private epoch: number = 0

Source

chain-test/src/unit/TestChaincodeStub.ts:125

Methods

createCompositeKey()

createCompositeKey(objectType, attributes): string

Parameters

objectType: string

attributes: string[]

Inherited from

ChaincodeStub.createCompositeKey

Source

chain-test/src/unit/TestChaincodeStub.ts:38


deletePrivateData()

deletePrivateData(collection, key): Promise\<void>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.deletePrivateData

Source

node_modules/fabric-shim/types/index.d.ts:138


getArgs()

getArgs(): string[]

Inherited from

ChaincodeStub.getArgs

Source

node_modules/fabric-shim/types/index.d.ts:100


getBinding()

getBinding(): string

Inherited from

ChaincodeStub.getBinding

Source

node_modules/fabric-shim/types/index.d.ts:113


getChannelID()

getChannelID(): string

Inherited from

ChaincodeStub.getChannelID

Source

node_modules/fabric-shim/types/index.d.ts:105


getClientIdentity()

getClientIdentity(caUser, mspId): ClientIdentity

Creates a client identity for the specified user and MSP.

Parameters

caUser: string

User identifier

mspId: string

Membership Service Provider ID

Returns

ClientIdentity for the specified user

Source

chain-test/src/unit/TestChaincodeStub.ts:186


getCreator()

getCreator(): SerializedIdentity

Inherited from

ChaincodeStub.getCreator

Source

node_modules/fabric-shim/types/index.d.ts:106


getDateTimestamp()

getDateTimestamp(): Date

Inherited from

ChaincodeStub.getDateTimestamp

Source

node_modules/fabric-shim/types/index.d.ts:112


getFunctionAndParameters()

getFunctionAndParameters(): object

Returns

fcn

fcn: string

params

params: string[]

Inherited from

ChaincodeStub.getFunctionAndParameters

Source

node_modules/fabric-shim/types/index.d.ts:102


getHistoryForKey()

getHistoryForKey(key): Promise\<HistoryQueryIterator> & AsyncIterable\<KeyModification>

Parameters

key: string

Inherited from

ChaincodeStub.getHistoryForKey

Source

node_modules/fabric-shim/types/index.d.ts:127


getMspID()

getMspID(): string

Inherited from

ChaincodeStub.getMspID

Source

node_modules/fabric-shim/types/index.d.ts:107


getPrivateData()

getPrivateData(collection, key): Promise\<Uint8Array>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.getPrivateData

Source

node_modules/fabric-shim/types/index.d.ts:135


getPrivateDataByPartialCompositeKey()

getPrivateDataByPartialCompositeKey(collection, objectType, attributes): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

collection: string

objectType: string

attributes: string[]

Inherited from

ChaincodeStub.getPrivateDataByPartialCompositeKey

Source

node_modules/fabric-shim/types/index.d.ts:143


getPrivateDataByRange()

getPrivateDataByRange(collection, startKey, endKey): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

collection: string

startKey: string

endKey: string

Inherited from

ChaincodeStub.getPrivateDataByRange

Source

node_modules/fabric-shim/types/index.d.ts:142


getPrivateDataHash()

getPrivateDataHash(collection, key): Promise\<Uint8Array>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.getPrivateDataHash

Source

node_modules/fabric-shim/types/index.d.ts:136


getPrivateDataQueryResult()

getPrivateDataQueryResult(collection, query): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

collection: string

query: string

Inherited from

ChaincodeStub.getPrivateDataQueryResult

Source

node_modules/fabric-shim/types/index.d.ts:144


getPrivateDataValidationParameter()

getPrivateDataValidationParameter(collection, key): Promise\<Uint8Array>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.getPrivateDataValidationParameter

Source

node_modules/fabric-shim/types/index.d.ts:141


getQueryResult()

getQueryResult(query): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

query: string

Inherited from

ChaincodeStub.getQueryResult

Source

node_modules/fabric-shim/types/index.d.ts:125


getQueryResultWithPagination()

getQueryResultWithPagination(query, pageSize, bookmark?): Promise\<StateQueryResponse\<StateQueryIterator>> & AsyncIterable\<KV>

Parameters

query: string

pageSize: number

bookmark?: string

Inherited from

ChaincodeStub.getQueryResultWithPagination

Source

node_modules/fabric-shim/types/index.d.ts:126


getSignedProposal()

getSignedProposal(): SignedProposal

Inherited from

ChaincodeStub.getSignedProposal

Source

node_modules/fabric-shim/types/index.d.ts:110


getStateByPartialCompositeKeyWithPagination()

getStateByPartialCompositeKeyWithPagination(indexKey, keyParts): Promise\<StateQueryResponse\<StateQueryIterator>> & AsyncIterable\<KV>

Retrieves state entries by partial composite key with pagination support.

Parameters

indexKey: string

Index key for the composite key

keyParts: string[]

Parts of the composite key to match

Returns

Iterable query response with pagination metadata

Overrides

ChaincodeStub.getStateByPartialCompositeKeyWithPagination

Source

chain-test/src/unit/TestChaincodeStub.ts:264


getStateByRange()

getStateByRange(start, end): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Retrieves state entries within a specified key range.

Parameters

start: string

Start key (inclusive)

end: string

End key (exclusive)

Returns

Iterable of key-value pairs within the range

Overrides

ChaincodeStub.getStateByRange

Source

chain-test/src/unit/TestChaincodeStub.ts:300


getStateByRangeWithPagination()

getStateByRangeWithPagination(startKey, endKey, pageSize, bookmark?): Promise\<StateQueryResponse\<StateQueryIterator>> & AsyncIterable\<KV>

Parameters

startKey: string

endKey: string

pageSize: number

bookmark?: string

Inherited from

ChaincodeStub.getStateByRangeWithPagination

Source

node_modules/fabric-shim/types/index.d.ts:121


getStateValidationParameter()

getStateValidationParameter(key): Promise\<Uint8Array>

Parameters

key: string

Inherited from

ChaincodeStub.getStateValidationParameter

Source

node_modules/fabric-shim/types/index.d.ts:119


getStringArgs()

getStringArgs(): string[]

Inherited from

ChaincodeStub.getStringArgs

Source

node_modules/fabric-shim/types/index.d.ts:101


getTransient()

getTransient(): Map\<string, Uint8Array>

Inherited from

ChaincodeStub.getTransient

Source

node_modules/fabric-shim/types/index.d.ts:108


getTxID()

getTxID(): string

Inherited from

ChaincodeStub.getTxID

Source

node_modules/fabric-shim/types/index.d.ts:104


getTxTimestamp()

getTxTimestamp(): Timestamp

Inherited from

ChaincodeStub.getTxTimestamp

Source

node_modules/fabric-shim/types/index.d.ts:111


invokeChaincode()

invokeChaincode(chaincodeName, args, channel): Promise\<ChaincodeResponse>

Mock implementation of chaincode invocation. Always returns a successful response for testing purposes.

Parameters

chaincodeName: string

Name of the chaincode to invoke (unused in mock)

args: string[]

Arguments to pass to the chaincode (unused in mock)

channel: string

Channel name (unused in mock)

Returns

Promise resolving to a successful ChaincodeResponse

Overrides

ChaincodeStub.invokeChaincode

Source

chain-test/src/unit/TestChaincodeStub.ts:327


mockCreator()

mockCreator(mspId, caUser): void

Sets the mock creator identity for transactions.

Parameters

mspId: string

Membership Service Provider ID

caUser: string

User identifier

Source

chain-test/src/unit/TestChaincodeStub.ts:206


mockState()

mockState(key, value): void

Adds a key-value pair to the mock blockchain state.

Parameters

key: string

State key

value: string

State value

Source

chain-test/src/unit/TestChaincodeStub.ts:196


purgePrivateData()

purgePrivateData(collection, key): Promise\<void>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.purgePrivateData

Source

node_modules/fabric-shim/types/index.d.ts:139


putPrivateData()

putPrivateData(collection, key, value): Promise\<void>

Parameters

collection: string

key: string

value: Uint8Array

Inherited from

ChaincodeStub.putPrivateData

Source

node_modules/fabric-shim/types/index.d.ts:137


setEvent()

setEvent(name, payload): void

Parameters

name: string

payload: Uint8Array

Inherited from

ChaincodeStub.setEvent

Source

node_modules/fabric-shim/types/index.d.ts:130


setPrivateDataValidationParameter()

setPrivateDataValidationParameter(collection, key, ep): Promise\<void>

Parameters

collection: string

key: string

ep: Uint8Array

Inherited from

ChaincodeStub.setPrivateDataValidationParameter

Source

node_modules/fabric-shim/types/index.d.ts:140


setStateValidationParameter()

setStateValidationParameter(key, ep): Promise\<void>

Parameters

key: string

ep: Uint8Array

Inherited from

ChaincodeStub.setStateValidationParameter

Source

node_modules/fabric-shim/types/index.d.ts:118


splitCompositeKey()

splitCompositeKey(compositeKey): SplitCompositekey

Parameters

compositeKey: string

Inherited from

ChaincodeStub.splitCompositeKey

Source

node_modules/fabric-shim/types/index.d.ts:133


createCompositeKey()

static createCompositeKey(objectType, attributes): string

Parameters

objectType: string

attributes: string[]

Inherited from

ChaincodeStub.createCompositeKey

Source

chain-test/src/unit/TestChaincodeStub.ts:38


deletePrivateData()

static deletePrivateData(collection, key): Promise\<void>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.deletePrivateData

Source

node_modules/fabric-shim/types/index.d.ts:138


deleteState()

static deleteState(key): Promise\<void>

Parameters

key: string

Inherited from

ChaincodeStub.deleteState

Source

node_modules/fabric-shim/types/index.d.ts:117


getArgs()

static getArgs(): string[]

Inherited from

ChaincodeStub.getArgs

Source

node_modules/fabric-shim/types/index.d.ts:100


getBinding()

static getBinding(): string

Inherited from

ChaincodeStub.getBinding

Source

node_modules/fabric-shim/types/index.d.ts:113


getChannelID()

static getChannelID(): string

Inherited from

ChaincodeStub.getChannelID

Source

node_modules/fabric-shim/types/index.d.ts:105


getCreator()

static getCreator(): SerializedIdentity

Inherited from

ChaincodeStub.getCreator

Source

node_modules/fabric-shim/types/index.d.ts:106


getDateTimestamp()

static getDateTimestamp(): Date

Inherited from

ChaincodeStub.getDateTimestamp

Source

node_modules/fabric-shim/types/index.d.ts:112


getFunctionAndParameters()

static getFunctionAndParameters(): object

Returns

fcn

fcn: string

params

params: string[]

Inherited from

ChaincodeStub.getFunctionAndParameters

Source

node_modules/fabric-shim/types/index.d.ts:102


getHistoryForKey()

static getHistoryForKey(key): Promise\<HistoryQueryIterator> & AsyncIterable\<KeyModification>

Parameters

key: string

Inherited from

ChaincodeStub.getHistoryForKey

Source

node_modules/fabric-shim/types/index.d.ts:127


getMspID()

static getMspID(): string

Inherited from

ChaincodeStub.getMspID

Source

node_modules/fabric-shim/types/index.d.ts:107


getPrivateData()

static getPrivateData(collection, key): Promise\<Uint8Array>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.getPrivateData

Source

node_modules/fabric-shim/types/index.d.ts:135


getPrivateDataByPartialCompositeKey()

static getPrivateDataByPartialCompositeKey(collection, objectType, attributes): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

collection: string

objectType: string

attributes: string[]

Inherited from

ChaincodeStub.getPrivateDataByPartialCompositeKey

Source

node_modules/fabric-shim/types/index.d.ts:143


getPrivateDataByRange()

static getPrivateDataByRange(collection, startKey, endKey): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

collection: string

startKey: string

endKey: string

Inherited from

ChaincodeStub.getPrivateDataByRange

Source

node_modules/fabric-shim/types/index.d.ts:142


getPrivateDataHash()

static getPrivateDataHash(collection, key): Promise\<Uint8Array>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.getPrivateDataHash

Source

node_modules/fabric-shim/types/index.d.ts:136


getPrivateDataQueryResult()

static getPrivateDataQueryResult(collection, query): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

collection: string

query: string

Inherited from

ChaincodeStub.getPrivateDataQueryResult

Source

node_modules/fabric-shim/types/index.d.ts:144


getPrivateDataValidationParameter()

static getPrivateDataValidationParameter(collection, key): Promise\<Uint8Array>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.getPrivateDataValidationParameter

Source

node_modules/fabric-shim/types/index.d.ts:141


getQueryResult()

static getQueryResult(query): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

query: string

Inherited from

ChaincodeStub.getQueryResult

Source

node_modules/fabric-shim/types/index.d.ts:125


getQueryResultWithPagination()

static getQueryResultWithPagination(query, pageSize, bookmark?): Promise\<StateQueryResponse\<StateQueryIterator>> & AsyncIterable\<KV>

Parameters

query: string

pageSize: number

bookmark?: string

Inherited from

ChaincodeStub.getQueryResultWithPagination

Source

node_modules/fabric-shim/types/index.d.ts:126


getSignedProposal()

static getSignedProposal(): SignedProposal

Inherited from

ChaincodeStub.getSignedProposal

Source

node_modules/fabric-shim/types/index.d.ts:110


getState()

static getState(key): Promise\<Uint8Array>

Parameters

key: string

Inherited from

ChaincodeStub.getState

Source

node_modules/fabric-shim/types/index.d.ts:115


getStateByPartialCompositeKey()

static getStateByPartialCompositeKey(objectType, attributes): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

objectType: string

attributes: string[]

Inherited from

ChaincodeStub.getStateByPartialCompositeKey

Source

node_modules/fabric-shim/types/index.d.ts:122


getStateByPartialCompositeKeyWithPagination()

static getStateByPartialCompositeKeyWithPagination(objectType, attributes, pageSize, bookmark?): Promise\<StateQueryResponse\<StateQueryIterator>> & AsyncIterable\<KV>

Parameters

objectType: string

attributes: string[]

pageSize: number

bookmark?: string

Inherited from

ChaincodeStub.getStateByPartialCompositeKeyWithPagination

Source

node_modules/fabric-shim/types/index.d.ts:123


getStateByRange()

static getStateByRange(startKey, endKey): Promise\<StateQueryIterator> & AsyncIterable\<KV>

Parameters

startKey: string

endKey: string

Inherited from

ChaincodeStub.getStateByRange

Source

node_modules/fabric-shim/types/index.d.ts:120


getStateByRangeWithPagination()

static getStateByRangeWithPagination(startKey, endKey, pageSize, bookmark?): Promise\<StateQueryResponse\<StateQueryIterator>> & AsyncIterable\<KV>

Parameters

startKey: string

endKey: string

pageSize: number

bookmark?: string

Inherited from

ChaincodeStub.getStateByRangeWithPagination

Source

node_modules/fabric-shim/types/index.d.ts:121


getStateValidationParameter()

static getStateValidationParameter(key): Promise\<Uint8Array>

Parameters

key: string

Inherited from

ChaincodeStub.getStateValidationParameter

Source

node_modules/fabric-shim/types/index.d.ts:119


getStringArgs()

static getStringArgs(): string[]

Inherited from

ChaincodeStub.getStringArgs

Source

node_modules/fabric-shim/types/index.d.ts:101


getTransient()

static getTransient(): Map\<string, Uint8Array>

Inherited from

ChaincodeStub.getTransient

Source

node_modules/fabric-shim/types/index.d.ts:108


getTxID()

static getTxID(): string

Inherited from

ChaincodeStub.getTxID

Source

node_modules/fabric-shim/types/index.d.ts:104


getTxTimestamp()

static getTxTimestamp(): Timestamp

Inherited from

ChaincodeStub.getTxTimestamp

Source

node_modules/fabric-shim/types/index.d.ts:111


invokeChaincode()

static invokeChaincode(chaincodeName, args, channel): Promise\<ChaincodeResponse>

Parameters

chaincodeName: string

args: string[]

channel: string

Inherited from

ChaincodeStub.invokeChaincode

Source

node_modules/fabric-shim/types/index.d.ts:129


purgePrivateData()

static purgePrivateData(collection, key): Promise\<void>

Parameters

collection: string

key: string

Inherited from

ChaincodeStub.purgePrivateData

Source

node_modules/fabric-shim/types/index.d.ts:139


putPrivateData()

static putPrivateData(collection, key, value): Promise\<void>

Parameters

collection: string

key: string

value: Uint8Array

Inherited from

ChaincodeStub.putPrivateData

Source

node_modules/fabric-shim/types/index.d.ts:137


putState()

static putState(key, value): Promise\<void>

Parameters

key: string

value: Uint8Array

Inherited from

ChaincodeStub.putState

Source

node_modules/fabric-shim/types/index.d.ts:116


setEvent()

static setEvent(name, payload): void

Parameters

name: string

payload: Uint8Array

Inherited from

ChaincodeStub.setEvent

Source

node_modules/fabric-shim/types/index.d.ts:130


setPrivateDataValidationParameter()

static setPrivateDataValidationParameter(collection, key, ep): Promise\<void>

Parameters

collection: string

key: string

ep: Uint8Array

Inherited from

ChaincodeStub.setPrivateDataValidationParameter

Source

node_modules/fabric-shim/types/index.d.ts:140


setStateValidationParameter()

static setStateValidationParameter(key, ep): Promise\<void>

Parameters

key: string

ep: Uint8Array

Inherited from

ChaincodeStub.setStateValidationParameter

Source

node_modules/fabric-shim/types/index.d.ts:118


splitCompositeKey()

static splitCompositeKey(compositeKey): SplitCompositekey

Parameters

compositeKey: string

Inherited from

ChaincodeStub.splitCompositeKey

Source

node_modules/fabric-shim/types/index.d.ts:133