Mock a CouchDB server for your unit tests.
Mock Couch will create an HTTP server that emulates the responses of a real CouchDB server. Since it is an actual HTTP server, no matter if you use libraries like cradle and nano, your code should work out of the box.
Mock Couch emit events, so you can listen to them to see the result of your test.
Installation
npm install --save-dev mock-couch
Changelog
- 0.1.3
- Several fixes (Thanks to @monowerker and @alexjeffburke)
- Rewriting a lot of the code using Ramda (to make it more functional)
- Support for linked documents in views (Credits to @monowerker)
- Implementation of
_changes
(Credits to @conor-mac-aoidh)
- 0.1.2
- Added HEAD requests. (Credits to @davidwood)
- Added option to prevent keep-alive connections. (Credits to @davidwood)
- Now is possible to use the query option
key
on views. - Other fixes.
- 0.1.1
- The emitted events are now standarized. Read about the events on the documentation.
- 0.1.0
- Support for views (map and reduce functions). Create a
_design/
document to use them. Read more about them here. - Documentation available on http://chris-l.github.io/mock-couch/
- Added
addDoc
method to add a document from the node.js side. Contrary to adding a document with a PUT, this is sync and allows you to specify the_rev
Documentation
Visit the Mock Couch website.
Features
- Implemented with restify.
- Uses simple JavaScript objects as documents.
- It emit events, so the tests can listen to them.
- The
mock_couch
object has adatabases
public property, to examine how the databases are in any moment. - Several of the CouchDB REST methods. Right now it has:
- GET one document
- GET
_all_docs
, including:include_docs=true
descending=true
startkey
endkey
- also, using
_all_docs
with POST to specify the desired keys
- GET the information of a database
- GET
_all_dbs
- GET
_uuids
- GET views (like
http://localhost:5984/database/_design/myviews/_view/someview/
) - PUT one document
- PUT a database
- POST one document
- POST to
_bulk_docs
multiple documents - DELETE one document
- DELETE a database
Not yet implemented
- deleting by setting the
_deleted
member _changes
- And a lot of other things!
Keep in mind that Mock Couch is not attempting to fully implement CouchDB, but only the features necessary for unit testing CouchDB based apps.
However, if there is a feature you need for your tests, feel free to add a feature request in the issues section!
Usage
Here is an example:
var mockCouch = ; // myfun is the function that we want to test.// It uses couchdb, either by using cradle, nano, or direct http requests// This function takes an object as parameter, maybe runs some validations// and if everything is ok, then save it on couchdb.var myfun = ; ;
If your testing requires the frequent setup and teardown of the mock server, it may be beneficial to prevent keep-alive connections. The server will always return a Connection: close
header if constructed with a keepAlive
option set to false
.
var couchdb = mockCouch.createServer({ keepAlive: false });
Status
Is still in its alpha stage, so its possible that it changes a lot.
License
MIT license
Contribution
Your feedback, pull requests, etc are welcomed! :)