MockDatabaseSpy class

A test double for DatabaseAdapter that records query history and allows configuring responses based on SQL substrings.

Use this to verify that your repository layer generates the expected SQL and correctly handles transaction lifecycles without a real database.

Implemented types

Constructors

MockDatabaseSpy([List<Map<String, dynamic>> _defaultData = const [], Map<String, List<Map<String, dynamic>>> smartResponses = const {}])
Creates the spy with optional defaultData for unmatched queries and smartResponses mapped by SQL substrings.

Properties

grammar Grammar
Returns the query grammar used by this adapter.
no setteroverride
hashCode int
The hash code for this object.
no setterinherited
history List<String>
Full log of all executed SQL statements in chronological order.
getter/setter pair
lastArgs List?
getter/setter pair
lastSql String
The last executed SQL statement, exposed for test assertions.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shouldFailTransaction bool
If true, operations within a transaction will throw an exception to simulate database failures and test rollback logic.
getter/setter pair
supportsTransactions bool
Indicates whether the adapter supports transactions.
no setteroverride
transactionHistory List<String>
Captures SQL statements executed specifically within the current transaction scope.
getter/setter pair

Methods

execute(String sql, [List? arguments]) Future<int>
Executes non-selecting commands (UPDATE, DELETE) or schema changes (DDL).
override
get(String sql, [List? arguments]) Future<Map<String, dynamic>>
Convenience wrapper to fetch the first result of a query.
override
getAll(String sql, [List? arguments]) Future<List<Map<String, dynamic>>>
Logs the query and returns pre-configured data if sql contains a key defined in _smartResponses; otherwise returns _defaultData.
override
insert(String table, Map<String, dynamic> values) Future
Inserts a record and returns the auto-generated primary key (if applicable).
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setMockData(Map<String, List<Map<String, dynamic>>> data) → void
Updates the mock response configuration at runtime.
toString() String
A string representation of this object.
inherited
transaction<T>(Future<T> callback(TransactionContext txn)) Future<T>
Executes a transaction scope.
override
watch(String sql, {List? parameters}) Stream<List<Map<String, dynamic>>>
Returns a static stream of _defaultData.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited