mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-14 07:00:51 -04:00
fix(curriculum): Made better tests for fortune teller lab (#62677)
This commit is contained in:
@@ -80,7 +80,19 @@ You should generate a random number between 1 and 5, inclusive, and assign it to
|
||||
|
||||
```js
|
||||
assert.isNotNull(randomNumber);
|
||||
assert.include([1,2,3,4,5], randomNumber);
|
||||
const originalRandom__test_code__ = Math.random;
|
||||
try {
|
||||
const mockValues__test_code__ = [0.0, 0.2, 0.4, 0.6, 0.8];
|
||||
let callCount__test_code__ = 0;
|
||||
Math.random = () => mockValues__test_code__[callCount__test_code__++ % mockValues__test_code__.length]
|
||||
const userCode__test_code__ = new Function(`${code};return randomNumber`);
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const result = userCode__test_code__()
|
||||
assert.strictEqual(result, i + 1);
|
||||
}
|
||||
} finally {
|
||||
Math.random = originalRandom__test_code__;
|
||||
}
|
||||
```
|
||||
|
||||
You should have a `selectedFortune` variable that is assigned a value based on the value of `randomNumber`.
|
||||
@@ -92,13 +104,20 @@ assert.isNotNull(selectedFortune);
|
||||
The `randomNumber` should correspond to its fortune. For example, if `randomNumber` is 1, the `selectedFortune` should be equal to `fortune1` and so on.
|
||||
|
||||
```js
|
||||
const condition1 = randomNumber === 1 && selectedFortune === fortune1;
|
||||
const condition2 = randomNumber === 2 && selectedFortune === fortune2;
|
||||
const condition3 = randomNumber === 3 && selectedFortune === fortune3;
|
||||
const condition4 = randomNumber === 4 && selectedFortune === fortune4;
|
||||
const condition5 = randomNumber === 5 && selectedFortune === fortune5;
|
||||
|
||||
assert.isTrue(condition1 || condition2 || condition3 || condition4 || condition5);
|
||||
const originalRandom__test_code__ = Math.random;
|
||||
try {
|
||||
const mockValues__test_code__ = [0.0, 0.2, 0.4, 0.6, 0.8];
|
||||
let callCount__test_code__ = 0;
|
||||
Math.random = () => mockValues__test_code__[callCount__test_code__++ % mockValues__test_code__.length];
|
||||
const fortunes__test_code__ = [fortune1, fortune2, fortune3, fortune4, fortune5,];
|
||||
const userCode__test_code__ = new Function(`${code};return selectedFortune`);
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const result = userCode__test_code__();
|
||||
assert.strictEqual(result, fortunes__test_code__[i]);
|
||||
}
|
||||
} finally {
|
||||
Math.random = originalRandom__test_code__;
|
||||
}
|
||||
```
|
||||
|
||||
You should output the `selectedFortune` to the console.
|
||||
|
||||
Reference in New Issue
Block a user