Skip to content

Commit

Permalink
Documentation and Support for Custom Mock variable
Browse files Browse the repository at this point in the history
  • Loading branch information
bvkimball committed Aug 11, 2015
1 parent 9a5b93f commit 8babcc6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,96 @@
# mockery

An attempt at creating a mock backend specifically for e2e and unit testing framework

### How does it work?

First you can add data to the 'state' variable which will be persisted through out the session.

```js
state.me = [{
"id": 100,
"firstName": "John",
"lastName": "Smith",
"email": "[email protected]",
"phone": "(617) 555 - 4321",
"address": {
"city": "Boston",
"state": "MA",
"zip": "02114"
}
}];
```

Then you can add routes for the Mock service, Here is an example of mocking a login call:

```js
Mockery.imitate('/login', 'POST', function (req, res) {
res.type('application/json');
if( state.me.email == req.body.username ){
res.statusCode(200);
res.json(state.me);
} else {
res.statusCode(404);
res.json({
"status": "not found"
});
}
});
```

### How can i look data up?

lodash is embedded within Mockery, you can use it like this:

```js
Mockery.imitate('/People/:id', 'GET', function (req, res) {
// Set the type of response, sets the Content-Type header.
res.type('application/json');
var person = _.find(state.people, {
'id': Number(req.params.id)
});
if (!person) {
return res.json(404, {
error: {
message: 'Person doesnt exist'
}
});
}

// Set the status code of the response.
res.statusCode(200);
return res.json(person);
});
```

### But what about PUT and POST?

lodash is embedded within Mockery, you can use it like this:

```js
Mockery.imitate('/People/:id', 'PUT', function (req, res) {
// Set the type of response, sets the Content-Type header.
res.type('application/json');
var person = _.find(state.people, {
'id': Number(req.params.id)
});

if (!person) {
return res.json(404, {
error: {
message: 'Person doesnt exist'
}
});
}
person = _.merge(person, req.data);
// drop the person and subsequently readd
_.reject(people, {
id: person.id
});
state.people.push(person);

// Set the status code of the response.
res.statusCode(200);
return res.json(person);
});
```
4 changes: 3 additions & 1 deletion dist/Mockery.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ var Mockery = (function () {
},
setup: {
value: function setup() {
var xhr = arguments[0] === undefined ? "XMLHttpRequest" : arguments[0];

if (supportsXHR) {
global.XHR = MockHttpRequest;
global[xhr] = MockHttpRequest;
}
if (supportsActiveX) {
global.ActiveXObject = function ActiveXObject(objId) {
Expand Down
4 changes: 2 additions & 2 deletions src/Mockery.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export class Mockery {
MockHttpRequest.addResponder(path, action, fn);
}

static setup() {
static setup(xhr='XMLHttpRequest') {
if (supportsXHR) {
global.XHR = MockHttpRequest;
global[xhr] = MockHttpRequest;
}
if (supportsActiveX) {
global.ActiveXObject = function ActiveXObject(objId) {
Expand Down

0 comments on commit 8babcc6

Please sign in to comment.