mock-variable
Version:
Generate fake / mock structured variable.
107 lines (69 loc) • 2.58 kB
Markdown
> [WIP] Generate fake / mock structured variable in a modern, human-readable way.
>
> 用一个现代的、可读的的方式来生成用于测试的假数据。
[](https://www.npmjs.com/package/mock-variable) [](https://travis-ci.org/hustcc/mock-variable) [](https://coveralls.io/github/hustcc/mock-variable) [](https://www.npmjs.com/package/mock-variable)
> **npm i --save mock-variable**
Then import it.
```js
import MV from 'mock-variable'; // ES6
var MV = require('mock-variable'); // ES5 with npm
```
You can assemble the variable structure arbitrarily by **using the mockers** below:
- **MV.bool()**
- **MV.number(min[, max = min, fixed = 0])**
- **MV.string([len = 8])**
- **MV.arrayOf(mocker[, min = 20, max = min])**
- **MV.shape(mockerObject)**
- **MV.oneOf(valueArray)**
- **MV.constant(value)**
- **MV.apply(Function)**
After you got the mocker, then use `mocker.mock()` to get the fake data.
You can see all the usage in the [test cases file](tests/test.js).
If more `Mocker` are needed, welcome to `send a pull request`, or put an issue to me.
Here is some examples. More you can see in [tests/test.js](tests/test.js) file.
- Simple usage
```js
MV.number(1, 9, 2).mock(); // 4.71
MV.string(6).mock(); // `Qv_teE`
MV.bool().mock(); // true / false
MV.oneOf(['hustcc', 'imcxl']).mock(); // random element from the array
MV.constant('hello, hustcc.').mock(); // `hello, hustcc`
MV.constant(null).mock(); // got null
MV.apply(function() { return Math.random(); } )).mock(); // will got number generate by fucntion Math.random()
```
- `arrayOf`
```js
var mocker = MV.arrayOf(VT.string(4), 10, 20);
mocker.mock(); // got an array which contains string, and array length 10 ~ 20.
```
- `shape`
```js
var mocker = MV.shape({
name: MV.string(10),
id: MV.number(10000, 1000000),
sex: MV.bool(),
city: 'hz',
});
mocker.mock(); // got a random value object.
```
- complex usage
```js
var mocker = MV.arrayOf({
name: MV.string(),
id: MV.number(10000, 1000000),
sex: MV.bool(),
city: 'hz',
});
mocker.mock(); // will got an array of users, and the list's length is 20.
```
```
npm i
npm t
```
ISC@[hustcc](https://github.com/hustcc).