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 :)
Most helpful comment
Not at all, go for it!