Skip to content

Commit

Permalink
Add test spies
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Dec 17, 2017
1 parent f9d8483 commit 7c244f6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Learn-Node-Tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Learn-Node-Tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"devDependencies": {
"expect": "^1.20.2",
"mocha": "^3.0.0",
"rewire": "^2.5.2",
"supertest": "^2.0.0"
},
"dependencies": {
Expand Down
10 changes: 10 additions & 0 deletions Learn-Node-Tests/spies/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var db = require('./db.js');

module.exports.handleSignup = (email, password) => {
// Check if the email exists
db.saveUser({
email,
password
});
// Sends the welcome email
};
24 changes: 24 additions & 0 deletions Learn-Node-Tests/spies/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const expect = require('expect');
const rewire = require('rewire');

var app = rewire('./app.js');

describe('App', () => {
var db = {
saveUser: expect.createSpy()
};
app.__set__('db',db);
it('should call saveUser with user object', () => {
var email = '[email protected]';
var password = '123abc';

app.handleSignup(email,password);
expect(db.saveUser).toHaveBeenCalled({email,password});
});

it('should call the spy correctly', () => {
var spy = expect.createSpy();
spy('Madhav',19);
expect(spy).toHaveBeenCalledWith('Madhav',19);
});
});
3 changes: 3 additions & 0 deletions Learn-Node-Tests/spies/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.saveUser = (user) => {
console.log('Saving the user', user);
};

0 comments on commit 7c244f6

Please sign in to comment.