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 trialFlorian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsWhy is the following quiz question answer incorrect?
This is the question:
The following test is failing right now with an Assertion Error: Expected undefined to be an object:
it(‘should update the status after a successful database call’, function (done) {
var status;
fetch(function () {
status = ‘success’;
expect(status).to.equal(‘success’);
});
});
Which of the following revisions would fix it?
Why is only the following answer correct?
it(‘should update the status after a successful database call’, function (done) {
var status;
fetch(function () {{
status = ‘success’;
done();
expect(status).to.equal(‘success’);
});
});
Why is the following answer wrong?
it(‘should update the status after a successful database call’, function (done) {
var status;
fetch(function () {
status = ‘success’;
expect(status).to.equal(‘success’);
done();
});
});
This is how Guil did it in the previous video and it doesn't seem to make a difference if you call 'done()' before or after 'expect()'.
Regards, Florian
5 Answers
Steven Parker
231,269 PointsI suspect the video is wrong, not the quiz. Calling expect before done still performs the test prematurely, but the call to done afterwards creates the the time delay that makes it seem like the test was performed later.
A better test would have caused the expected condition to be false until after the timer elapsed.
Emeka Okoro
Courses Plus Student 11,724 PointsYeah, I think he made a slight error in calling done after the expectation. That said, he did a great job running through the topic
Hakim Rachidi
38,490 PointsThis should give us a timeout error since done was passed to the unit test, mocha has to wait until done is called.
it(‘should update the status after a successful database call’, function (done) {
var status;
fetch(function () {
status = ‘success’;
expect(status).to.equal(‘success’);
});
});
Calling done before one or more expectations, mocha cannot register the result of the exceptation. So I think that the quiz is wrong but the video is right.
it(‘should update the status after a successful database call’, function (done) {
var status;
fetch(function () {
status = ‘success’;
done(); // everything after the call of done doesn't get registered
expect(false).to.be.ok; // no exception
});
});
Aaron Bell
6,113 PointsThe name of the link made it make sense. You want the function to be 'done' before you pass your expectations onto it. If you pass your expectations onto it before it's done, you'll get the wrong results.
Hakim Rachidi
38,490 PointsAaron Bell I've forgotten the most important part of my answer. Here is the updated one.
William Blair
Courses Plus Student 3,542 PointsThe answer is wrong because you aren't executing the stub before your expectation. You've essentially turned what was supposed to be an asynchronous test into a synchronous one.
stanislavmilev
Courses Plus Student 14,723 PointsI was about to ask the same question. still don't quite get what we have tested
saveGame get passed a callback function. That function (the one we pass) gets called/executed from within the saveGame function.
In the test, we call saveGame and pass our own function as a parameter to it. Are we saying we are defining saveGame function behaviour from within a test? What are we testing then? The fact that saveGame executed our code? If not, why would we ever need to pass saveGame a callback function?
I also don't get why expectations are a part of the function we pass to saveGame.
Can anyone clearly explain this?
Hakim Rachidi
38,490 PointsHakim Rachidi
38,490 PointsIf the expectation fails there is no way the test can register it. The later example code works not only fine but it also make way more sense.