Seriate.mock
=========
An addon library for Seriate that provides request mocking.
API
The follow methods are added to the seriate
instance:
method name | description |
---|---|
addMock(key, config) |
Stores the mock in the cache to be retrieved later |
addFileMock(path, config) |
Stores a mock for use when sql.fromFile() is called |
clearMock([key]) |
Removes specific mock from cache if key is provided or clears all mocks if no key is provided. |
getMock(key) |
Retrieves mock from cache |
resolveMock(stepName, stepConfig) |
Retrieves the correct mock to be used for a particular SQL Context step. |
Examples
Seriate.mock provides a factory function that modifies a seriate
object.
var sql = ;var mock = ; ; sql; sql ;
The value passed to addMock
can take several forms:
// ... sql; // Will return the array sql; // Will return the numeric value // ... sql; sql ;
Warning: Giving addMock
a plain object will not work the way you think it does.
When passing a plain object to addMock
it must be in the form of a raw mock object including at least the mockResults
key:
// ... sql;
The API works the same way with Seriate's TransactionContext
object:
sql; sql ;
Warning: Using this library will mock all transaction contexts. You will not be able to perform actual transactions at any point.
You can mock calls to execute
or first
by using the query (or preparedSql) as the key:
sql; sql ; sql ;
You might consider using the once
parameter single use mocks to avoid collisions without having to remember to clear the mock manually.
sql; sql ; sql ;
File Mocking
You can mock by file access by using the addFileMock
method.
var expectedResults = name: "Neo" name: "Trinity" name: "Morpheus" ; sql; sql ;
The file mocks will try to resolve to the same absolute path as the value given to sql.fromFile
. Set the configuration option sqlFileBasePath
to form the basis for your absolute paths built by addFileMock
. For instance;
// Filename: /Users/me/myProject/src/lib/somefile.js sql ; // File will resolve to /Users/me/myProject/src/lib/matrixCharacters
To mock this file from an external test, you would do something like this:
// Filename: /Users/me/myProject/spec/lib/somefile.spec.js var sql = ;var mock = ; ; var expectedResults = name: "Neo" name: "Trinity" name: "Morpheus" ; sql; // Mock file will resolve to /Users/me/myProject/src/lib/matrixCharacters