Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Testing #99

Merged
merged 7 commits into from
Jun 20, 2017
Merged

Testing #99

merged 7 commits into from
Jun 20, 2017

Conversation

abhinadduri
Copy link
Collaborator

Finished testing for storage modules

Copy link
Contributor

@dannycoates dannycoates left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start. I left a few specific comments, but they also apply to the rest. With sinon we can make detailed assertions about what we expect our code to do in specific situations. Finding the balance between tests that are too general and too specific takes practice. One rule of thumb is to not make assertions about the specific implementation but to assert the results and side effects you expect as someone calling the function.

package.json Outdated
"redis": "^2.7.1"
"proxyquire": "^1.8.0",
"redis": "^2.7.1",
"sinon": "^2.3.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets move proxyquire and sinon to devDependencies

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved them to dev dependencies

conf.bitly_key = null;
s3Stub.upload.callsArgWith(1, null, {});
return storage.set('123', {}, 'Filename.moz', 'url.com')
.then(reply => assert(reply.url === 'url.com' && reply.uuid !== null))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should make this into multiple assertions:assert.equal(reply.url, 'url.com') ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also use sinon to override crypto.randomBytes to return a specific Buffer that we can assert against, something like this:

const buffer = new Buffer(10)
sinon.stub(crypto, 'randomBytes').returns(buffer)
...
assert.equal(reply.uuid, buffer.toString('hex'))
crypto.randomBytes.restore()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this function has multiple side effects it's useful to assert that the ones we expect to get called actually were. For example, setting the expire time in redis. We can use sinon to assert that it was called, and with the arguments we expect:

assert(expire.calledOnce)
assert(expire.calledWith('123', 86400000))

ref: http://sinonjs.org/releases/v2.3.4/spies/

For this to work you might need to create a beforeEach to call expire.reset() etc.

it('Filesize returns properly if id exists', function() {
s3Stub.headObject.callsArgWith(1, null, {ContentLength: 1});
return storage.length('123')
.then(reply => assert(reply === 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets prefer assert.equal when possible. It'll print better messages when the assertion fails, including the actual and expected values.


it('Should error when the file does not exist', function() {
s3Stub.getObject.returns({
createReadStream: function() { return null; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be a spy that throws to more closely match the actual behavior

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored

@pdehaan
Copy link
Contributor

pdehaan commented Jun 19, 2017

We should also tweak the .eslintrc.yml file to recognize the mocha specific keywords:

diff --git a/.eslintrc.yml b/.eslintrc.yml
index 16f7f63..b98f50c 100644
--- a/.eslintrc.yml
+++ b/.eslintrc.yml
@@ -2,6 +2,7 @@ env:
   browser: true
   es6: true
   jquery: true
+  mocha: true
   node: true

 extends:

@@ -101,7 +103,7 @@ app.post('/delete/:id', (req, res) => {

storage
.delete(id, delete_token)
.then(err => {
.then(() => {
if (!err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think err is now undefined here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this in a new push!

@dannycoates dannycoates merged commit 2effdf2 into master Jun 20, 2017
@dannycoates dannycoates deleted the testing branch June 20, 2017 20:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants