Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAaron Bell
6,113 PointsHow does this spec not check for subtraction's desired behavior?
I feel like this should check subtraction throws an error. I would prefer a more useful error message so that I know where I'm messing up (or if I'm actually messing up).
It's for this coding challenge here: https://teamtreehouse.com/library/javascript-unit-testing/improving-our-tests/catching-an-error.
var expect = require('chai').expect
describe('subtraction', function () {
var subtraction = require('../WHEREVER')
it('only works with numbers', function () {
// YOUR CODE HERE
expect(subtraction('a', 1)).to.throw();
expect(subtraction(1, 'a')).to.throw();
expect(subtraction('b', 'a')).to.throw();
expect(subtraction([1.3], 'a')).to.throw();
expect(subtraction(2, 1)).to.not.throw();
})
})
function subtraction (number1, number2) {
if (typeof number1 !== 'number' || typeof number2 !== 'number') {
throw Error('subtraction only works with numbers!')
}
return number1 - number2
}
5 Answers
Steven Parker
231,275 PointsThe argument to "expect" needs to be a function that will cause "subtraction" to be called. If you call it yourself in the argument, it will be done (exception or not) and the argument passed to "expect" will be the result.
Eden Gomez
22,787 Pointsvar expect = require('chai').expect
describe('subtraction', function () {
var subtraction = require('../WHEREVER')
it('only works with numbers', function () {
// YOUR CODE HERE
//var =
expect(substraction(number1, number2).to.be.NaN);
})
})
Eden Gomez
22,787 Pointsjs''' n '''
Eden Gomez
22,787 Pointsvar expect = require('chai').expect
describe('subtraction', function () { var subtraction = require('../WHEREVER')
it('only works with numbers', function () {
// YOUR CODE HERE
expect(substraction(number1, number2).to.be.NaN); })
})
Im sorry but im lost, can somebody give us a hint??? i dont know what method should i use
Steven Parker
231,275 PointsTo get the best chance of a community response, always start a fresh question instead of asking one as an "answer" to another question.
Eden Gomez
22,787 PointsAlready did it Steven Parker Iām still stuck in that challenge. Do you mind give me an extra hint?šš