0.0.2 • Published 7 years ago

mock-variable v0.0.2

Weekly downloads
55
License
MIT
Repository
github
Last release
7 years ago

mock-variable

WIP Generate fake / mock structured variable in a modern, human-readable way.

用一个现代的、可读的的方式来生成用于测试的假数据。

Ver Build Status Coverage Status npm download

1. Install

npm i --save mock-variable

Then import it.

import MV from 'mock-variable'; // ES6
var MV = require('mock-variable'); // ES5 with npm

2. API & Mocker

You can assemble the variable structure arbitrarily by using the mockers below:

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.

If more Mocker are needed, welcome to send a pull request, or put an issue to me.

3. Usage examples

Here is some examples. More you can see in tests/test.js file.

  • Simple usage
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
var mocker = MV.arrayOf(VT.string(4), 10, 20);

mocker.mock(); // got an array which contains string, and array length 10 ~ 20.
  • shape
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
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.

4. Test

npm i

npm t

License

ISC@hustcc.