0
0
Blockchain / Solidityprogramming~20 mins

Writing test cases in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Blockchain Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple smart contract test
Consider this Solidity test snippet using Hardhat framework. What will be the output printed by the test runner?
Blockchain / Solidity
describe('SimpleStorage', function () {
  it('should store and retrieve the value', async function () {
    const SimpleStorage = await ethers.getContractFactory('SimpleStorage');
    const storage = await SimpleStorage.deploy();
    await storage.deployed();

    await storage.store(42);
    const value = await storage.retrieve();
    console.log('Stored value:', value.toString());
  });
});
AStored value: 42
BStored value: 0
CTypeError: value.toString is not a function
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Remember that retrieve() returns a BigNumber and toString() converts it to a string.
Predict Output
intermediate
1:30remaining
Test case result for failing assertion
What will happen when running this test case in a blockchain test framework?
Blockchain / Solidity
it('should fail when value is incorrect', async function () {
  const value = 10;
  assert.equal(value, 20, 'Value should be 20');
});
ATypeError: assert.equal is not a function
BTest passes silently
CSyntaxError: missing semicolon
DTest fails with message: Value should be 20
Attempts:
2 left
💡 Hint
assert.equal compares two values and throws if they differ.
🔧 Debug
advanced
2:30remaining
Identify the error in this test case
This test case is supposed to check if a transaction reverts. What error will it produce when run?
Blockchain / Solidity
it('should revert on invalid input', async function () {
  await expect(contract.doSomething(0)).to.be.revertedWith('Invalid input');
});
ATest passes successfully
BTypeError: expect(...).to.be.revertedWith is not a function
CSyntaxError: Unexpected token await
DError: contract.doSomething is not a function
Attempts:
2 left
💡 Hint
Check if the testing framework supports the 'revertedWith' matcher.
Predict Output
advanced
3:00remaining
Output of asynchronous test with event listening
What will this test print to the console when run?
Blockchain / Solidity
it('should emit event on action', async function () {
  const tx = await contract.performAction();
  const receipt = await tx.wait();
  const event = receipt.events.find(e => e.event === 'ActionPerformed');
  console.log('Event data:', event.args[0].toString());
});
ATypeError: receipt.events.find is not a function
BEvent data: 123
CEvent data: undefined
DSyntaxError: Unexpected identifier
Attempts:
2 left
💡 Hint
TransactionReceipt in ethers.js has 'logs' property, not 'events'.
🚀 Application
expert
1:30remaining
Number of test cases needed for full coverage
A smart contract function has 3 independent boolean inputs. How many test cases are needed to cover all input combinations?
A3
B6
C8
D9
Attempts:
2 left
💡 Hint
Each boolean can be true or false independently.