Skip to content

Commit

Permalink
feat: support mock class method from instance (#52)
Browse files Browse the repository at this point in the history
* fix: read file on windows
  • Loading branch information
fengmk2 committed Mar 24, 2020
1 parent e88430f commit d2e4c28
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -306,6 +306,25 @@ req.on('error', function (err) {
});
```

### .classMethod(instance, method, mockMethod)

```js
class Foo {
async fetch() {
return 1;
}
}

const foo = new Foo();
const foo1 = new Foo();

mm.classMethod(foo, 'fetch', async () => {
return 3;
});
assert(await foo.fetch() === 3);
assert(await foo1.fetch() === 3);
```

## License

```
Expand Down
5 changes: 5 additions & 0 deletions lib/es6.js
Expand Up @@ -78,3 +78,8 @@ mm.errorOnce = function(mod, method, error, props, timeout) {
});
return this;
};

// mock class method from instance
mm.classMethod = function(instance, property, value) {
return mm(instance.constructor.prototype, property, value);
};
40 changes: 40 additions & 0 deletions test/es6.js
@@ -1,5 +1,6 @@
'use strict';

const assert = require('assert');
const mm = require('..');

describe('test/es6.test.js', () => {
Expand Down Expand Up @@ -36,6 +37,45 @@ describe('test/es6.test.js', () => {
});
});

describe('classMethod()', () => {
it('should class method from instance', async () => {
class Foo {
async fetch() {
return 1;
}
}

const foo = new Foo();
const foo1 = new Foo();
assert(await foo.fetch() === 1);
assert(await foo1.fetch() === 1);

mm(foo, 'fetch', async () => {
return 2;
});
assert(await foo.fetch() === 2);
assert(await foo1.fetch() === 1);

mm.restore();
mm.classMethod(foo, 'fetch', async () => {
return 3;
});
assert(await foo.fetch() === 3);
assert(await foo1.fetch() === 3);

mm.restore();
mm.classMethod(foo, 'fetch', async () => {
return 4;
});
const foo2 = new Foo();
assert(await foo.fetch() === 4);
assert(await foo1.fetch() === 4);
assert(await foo2.fetch() === 4);
const foo3 = new Foo();
assert(await foo3.fetch() === 4);
});
});

describe('error(), errorOnce()', () => {
it('should mock error', function* () {
mm.error(foo, 'getValue');
Expand Down
18 changes: 9 additions & 9 deletions test/mm.test.js
Expand Up @@ -138,15 +138,15 @@ describe('test/mm.test.js', () => {
describe('error(), errorOnce()', () => {
it('should mock fs.readFile return error', function(done) {
mm.error(fs, 'readFile', 'can not read file');
fs.readFile('/etc/hosts', 'utf8', function(err, data) {
fs.readFile(__filename, 'utf8', function(err, data) {
assert(err);
err.name.should.equal('MockError');
err.message.should.equal('can not read file');
assert(!data);

mm.restore();

fs.readFile('/etc/hosts', 'utf8', function(err, data) {
fs.readFile(__filename, 'utf8', function(err, data) {
assert(!err);
assert(data);
data.should.containEql('127.0.0.1');
Expand All @@ -157,13 +157,13 @@ describe('test/mm.test.js', () => {

it('should mock fs.readFile return error once', done => {
mm.errorOnce(fs, 'readFile', 'can not read file');
fs.readFile('/etc/hosts', 'utf8', (err, data) => {
fs.readFile(__filename, 'utf8', (err, data) => {
assert(err);
err.name.should.equal('MockError');
err.message.should.equal('can not read file');
assert(!data);

fs.readFile('/etc/hosts', 'utf8', (err, data) => {
fs.readFile(__filename, 'utf8', (err, data) => {
assert(!err);
assert(data);
data.should.containEql('127.0.0.1');
Expand All @@ -176,7 +176,7 @@ describe('test/mm.test.js', () => {
const err = new Error('mock error instance');
err.name = 'CustomError';
mm.error(fs, 'readFile', err);
fs.readFile('/etc/hosts', 'utf8', function(err, data) {
fs.readFile(__filename, 'utf8', function(err, data) {
assert(err);
err.name.should.equal('CustomError');
err.message.should.equal('mock error instance');
Expand All @@ -187,7 +187,7 @@ describe('test/mm.test.js', () => {

it('should mock error with empty error', function(done) {
mm.error(fs, 'readFile');
fs.readFile('/etc/hosts', 'utf8', function(err, data) {
fs.readFile(__filename, 'utf8', function(err, data) {
assert(err);
err.name.should.equal('MockError');
err.message.should.equal('mm mock error');
Expand All @@ -198,7 +198,7 @@ describe('test/mm.test.js', () => {

it('should mock error with properties', function(done) {
mm.error(fs, 'readFile', 'mm mock error', { code: 'ENOENT', name: 'MockError' });
fs.readFile('/etc/hosts', 'utf8', function(err, data) {
fs.readFile(__filename, 'utf8', function(err, data) {
assert(err);
err.name.should.equal('MockError');
err.message.should.equal('mm mock error');
Expand All @@ -211,7 +211,7 @@ describe('test/mm.test.js', () => {
it('should mock error with 500ms timeout', function(done) {
mm.error(fs, 'readFile', '500ms timeout', 500);
const start = Date.now();
fs.readFile('/etc/hosts', 'utf8', function(err, data) {
fs.readFile(__filename, 'utf8', function(err, data) {
const use = Date.now() - start;
assert(err);
err.name.should.equal('MockError');
Expand All @@ -225,7 +225,7 @@ describe('test/mm.test.js', () => {
it('should mock error with 500ms timeout and properties', function(done) {
mm.error(fs, 'readFile', '500ms timeout', { code: 'ENOENT' }, 500);
const start = Date.now();
fs.readFile('/etc/hosts', 'utf8', function(err, data) {
fs.readFile(__filename, 'utf8', function(err, data) {
const use = Date.now() - start;
assert(err);
err.name.should.equal('MockError');
Expand Down

0 comments on commit d2e4c28

Please sign in to comment.