Lesson 9 - Winner is always the same in unit test? #2581
-
Hi, In unit test of Lesson 9, why is the winner always the same? In video, Patrick ran the test once and found out who the winner is, then we directly use the index to get that user's balance for assertion: const winnerBalance = await accounts[2].getBalance()
// ... other codes
assert.equal(
winnerBalance.toString(),
startingBalance
.add(
raffleEntranceFee
.mul(additionalEntrances)
.add(raffleEntranceFee)
)
.toString()
) No matter how many times we run this unit test, the winner will always be the same player. Why? One reason I can think of is that, with the same input, the algorithm for generating the random word will always return the same result. Is this true? Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @levblanc since we are using a Mock in our local environment, we expect the winner to always be the same because the mock doesn't generate randomness. However in a testnet or mainnet, you are guaranteed to get a random winner. |
Beta Was this translation helpful? Give feedback.
-
This is a unit test, and unit tests are done locally. It follows that we then use a Mock (we use it for local networks), which leads to the same winner (the Mock cannot generate randomness; an off-chain solution--Chainlink--is required to do so) being selected everytime. If you run the staging test though, a different winner will be selected as we are interacting w/ a oracle off-chain (Chainlink VRF to generate randomness) in this case. |
Beta Was this translation helpful? Give feedback.
Hey @levblanc since we are using a Mock in our local environment, we expect the winner to always be the same because the mock doesn't generate randomness. However in a testnet or mainnet, you are guaranteed to get a random winner.
Checkout this explanation by Patrick.