mocks / 0.0.15 last updated 2 years ago created on Feb 13th 2012
Install
npm install --save mocks
Clone
git clone git@github.com:vojtajina/node-mocks.git
dependencies
No dependencies listed.
maintainers
versions
15 total
| 0.0.15 | 2 years ago | vojtajina |
| 0.0.14 | 2 years ago | vojtajina |
| 0.0.13 | 2 years ago | vojtajina |
| 0.0.12 | 2 years ago | vojtajina |
| 0.0.11 | 3 years ago | vojtajina |
| 0.0.10 | 3 years ago | vojtajina |
| 0.0.9 | 3 years ago | vojtajina |
| 0.0.8 | 3 years ago | anonymous |
| 0.0.7 | 3 years ago | anonymous |
| 0.0.6 | 3 years ago | anonymous |
| 0.0.5 | 3 years ago | vojtajina |
| 0.0.4 | 3 years ago | vojtajina |
| 0.0.3 | 4 years ago | vojtajina |
| 0.0.2 | 4 years ago | vojtajina |
| 0.0.1 | 4 years ago | vojtajina |
readme
Node Mocks 
Set of mocks and utilities for easier unit testing with Node.js.
See http://howtonode.org/testing-private-state-and-mocking-deps for better explanation.
Prerequisites
Installation
Example
Mocking a file system
Faking randomness
Non-blocking I/O operations can return in random order. Let's say you read a content of two files (asynchronously). There is no guarantee, that you get the content in right order. That's fine, but we want to test our code, whether it can handle such a situation and still work properly. In that case, you can use predictableNextTick, which process callbacks depending on given pattern.
This test will always pass. That's cool, as we like to see tests passing. The bad thing is, that it does not work in production, with real file system, as it might return in different order...
So, we need to test, whether our app works even when the fs returns in random order. Having randomness in unit tests is not good habit, as it leads to flaky tests.
Let's change the previous unit test to this:
Now, the unit test fails, because our fake file system calls back in different order. Note, it's not random, as you explicitly specified the pattern (2, 0, 1), so it the fake fs will consistently call back in this order: /three.js, /one.js, two.js.
API
Currently supported API is only very small part of real Node's API. Basically I only implemented methods I need for testing Testacular.
I will keep adding more and of course if anyone wants to help - pull requests are more than welcomed.
fs
- stat(path, callback)
- readdir(path, callback)
- readFile(path [, encoding], callback)
- readFileSync(path)
- watchFile(path [, options], callback)
- _touchFile(path, mtime, content) *
http
- http.ServerResponse
- http.ServerRequest