Test a smart contract, including the things that should fail
Blockchain learning series 9 of 12
Test duplicate submissions, invalid input, separate callers and unchanged state.

Read the infographic as text
Test a smart contract, including the things that should fail
Test duplicate submissions, invalid input, separate callers and unchanged state.
- Successful call
- The expected record and event appear.
- Rejected call
- Zero and duplicate fingerprints fail.
- Independent caller
- Another address can register the same fingerprint separately.
Learning goal: Test duplicate submissions, invalid input, separate callers and unchanged state.
Blockchain learning series 9 of 12. Educational diagrams; not a production architecture.
A contract that accepts one valid submission has passed only its easiest test. The more useful question is whether it rejects invalid actions without damaging an existing record. Our register is small enough to check its entire intended behaviour.
Start with the completed contract lesson. Keep using a simulation. This guide introduces tests as executable statements about behaviour, not as a percentage score that proves safety.
Write the expectations before running them
For our design, an address can record a non-zero fingerprint once. Another address can record the same fingerprint independently. A changed document creates a different fingerprint and therefore a separate entry. A failed duplicate must leave the original record unchanged.
These are product rules, not universal blockchain rules. Another application might deliberately restrict registration to approved issuers or permit corrections. Its test cases would need to follow those requirements.
Test the intended rules
| Test | Expected outcome | Reason |
|---|---|---|
| Absent record | Zero | No stored entry for that pair |
| Zero fingerprint | Reject | Explicit input rule |
| Duplicate caller and fingerprint | Reject; first block unchanged | No overwrite |
| Second caller, same fingerprint | Allow | Records are caller-scoped |
| Changed document | Separate fingerprint and entry | Exact byte comparison |
| Ordinary direct ETH transfer | Reject | No payable receive or fallback function |
Perform the cases manually
In Remix VM, deploy a fresh copy and select account A. Read the practice fingerprint from the previous lesson: the result should be zero. Register it, wait for successful execution and read it again. Save the returned block number.
Attempt the same registration from A. Remix may reject it during estimation before sending, or show a reverted execution if you explicitly send it. In either case, read the record again: its first number must be unchanged. These are different execution paths with the same intended protection.
Switch to account B and read A's entry by supplying A's address. A public getter is not restricted to the record's submitter. Read B's entry and expect zero, then register from B and expect a positive number for B. Public access to the record is part of this design.
Finally, try the all-zero fingerprint. It should fail. Use a new non-zero fingerprint from A and expect another successful entry. Do not use real documents with confidential information merely to make the exercise feel realistic.
Run the automated version
In the downloaded lab folder, run:
Code · sh
npm testThe test compiles the exact contract, creates a fresh simulated chain and deploys it. It checks an absent record, zero-input rejection, successful storage, the event fields, duplicate rejection and unchanged state. It also checks a second caller, changed document bytes and an ordinary direct ETH transfer that should be rejected.
Here is the core duplicate check from the test. firstBlock is saved after the first successful registration; fingerprint is a calculated digest.
Code · javascript
await assert.rejects(
contract.register.staticCall(fingerprint),
/Already recorded/
);
assert.equal(
await contract.recordedAt(alice.address, fingerprint),
firstBlock
);A static call simulates execution without committing state. The complete test also forces a duplicate transaction to be mined with a gas limit, checks the failed receipt path and then confirms the original value remains. That matters because an interface must handle both preflight rejection and execution failure. The ethers contract API documents these call methods.
Interpret a failure honestly
If a test fails, read which expectation failed before changing the assertion. An unexpected success in a rejection test can expose a missing rule. A connection error can mean the environment failed before the contract was exercised. Treating both as a reassuring red result defeats the purpose.
Change the contract's duplicate rule temporarily, run the tests and observe which expectation catches the change. Then restore the original source and run again. This exercise demonstrates that the test can detect the failure it claims to cover.
Remix's unit-testing plugin offers another way to automate Solidity tests. We supply a Node-based runner here so the same example can test deployment and browser-facing code together. You do not need to master both systems to continue.
What a passing test does not establish
The suite verifies the stated behaviours in the supplied environment. It is not an audit, a formal proof or a stress test of a public network. It does not resolve identity, legal claims, privacy obligations, supplier outages or malicious website releases.
For larger applications, tests must cover permissions, external calls, economic assumptions and operational failures relevant to their design. The small register avoids many of those mechanisms deliberately.
You are ready to continue when you can name the expected result before each test and explain why a second caller can register the same fingerprint. If your answer is that the first registration grants ownership, return to the contract's actual rules.
Sources
Source note: We have made reasonable efforts to provide sources for material claims. Some third-party sources could not be accessed at publication.
Related reading
Guides ·
Accounts, wallets and smart contracts: the parts of a blockchain app
Distinguish an address, a signing tool and a programme with persistent state.
Guides ·
Build your first smart contract: a document fingerprint register
Deploy a small Solidity contract and explain exactly what it records.
Guides ·
Connect a website to your smart contract
Run a local interface that hashes files, submits a transaction and reads the receipt.
Guides ·
Why decentralised apps still use centralised cloud services
A smart contract can run across a distributed network while the website, data services and access points depend on a handful of providers. Resilience depends on the complete application.

