Freecodecamp: Add test to Set the child prototype... challenge to check for correct output

Created on 9 Mar 2020  路  14Comments  路  Source: freeCodeCamp/freeCodeCamp

We could try and add an additional test that would make sure beagle.eat() logs "nom nom nom" to the console

_Originally posted by @RandellDawson here_

first timers only help wanted learn

Most helpful comment

Do you mind if I push this or will you do that?

Not at all, go for it!

All 14 comments

Can you explain exactly where the issue is

The issue i see is that, if someone poorly implements the eat function, they don't get feedback that they did something wrong. Take this code for example:

// Add your code below this line
Dog.prototype = Object.create(Animal.prototype)
Dog.prototype.constructor = Dog
Dog.prototype.bark = function (){
    let food = 10/0
}
// Add your code above this line

let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"

Console output:

"nom nom nom"

Still passing the lession, although im deviding by zero ;)

We could spy on the console and assert, that after beagle.eat() is called, the spy got called once with "nom nom nom".
Since we are using chai allready, i assumed we could just use it in the markdown files.
My code so far (./curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.english.md):

  - text: <code>beagle.eat()</code> should print <code>"nom nom nom"</code>
    testString: |
      const spy = chai.spy.on(console,'log');
      beagle.eat();
      expect(spy).to.have.been.called.with("nom nom nom");

Maybe it's helping someone :)

@marioweid it's a nice idea, but we only use the core chai package, not chai-spies. A custom solution where you proxy console.log should work, albeit with a little more effort.

I tried doing this

  - text: <code>beagle.eat()</code> should print <code>"nom nom nom"</code>
    testString: |
      console.log = function(msg){throw msg;}
      expect(beagle.eat()).to.throw("nom nom nom");

Unfortunately, this does not work.

@hassaanp that's almost right and it's a neat approach. I played with it for a bit and came up with:

  - text: <code>beagle.eat()</code> should log <code>"nom nom nom"</code>
    testString: |
      console.log = function(msg){throw msg;}
      assert.throws(() => beagle.eat(),"nom nom nom");

Since each test is isolated, there's no need to restore the original console.log.

@ojeytonwilliams awesome!
Do you mind if I push this or will you do that?

Do you mind if I push this or will you do that?

Not at all, go for it!

@hassaanp
I guess you can add the change for all languages :)

I guess you can add the change for all languages :)

@marioweid No need to do that. We are working to using the English version of the file's textString to work across all languages. See PR #38040 for more information.

@hassaanp Once we merge your PR, you can find all the challenges where we need to test for a specific value being logged to the console.

@hassaanp Once we merge your PR, you can find all the challenges where we need to test for a specific value being logged to the console.

Acknowledged. Thanks for the heads up. I will push the fixes for all such instances.

@hassaanp Make sure to put the others on a different PR, so as not to hold up your existing PR.

@RandellDawson
of course :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robwelan picture robwelan  路  3Comments

QuincyLarson picture QuincyLarson  路  3Comments

bagrounds picture bagrounds  路  3Comments

MelissaManning picture MelissaManning  路  3Comments

trashtalka3000 picture trashtalka3000  路  3Comments