I am following the pet shop tutorial on Truffle. The first few steps work, i.e., I can compile and migrate the Adoption contract. My software versions are:
Truffle v5.4.33 (core: 5.4.33) Ganache v7.0.1 Solidity v0.5.16 (solc-js) Node v17.6.0 Web3.js v1.5.3
Adoption contract code:
pragma solidity ^0.5.0;
contract Adoption {
address[16] public adopters;
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 5);
adopters[petId] = msg.sender;
return petId;
}
function getAdopters() public view returns (address[16] memory) {
return adopters;
}
}
Migrations folder: 1_initial_migration.js
var Migrations = artifacts.require("./Migrations.sol");
module.exports = function(deployer) {
deployer.deploy(Migrations);
};
2_deploy_contracts.js
var Adoption = artifacts.require("Adoption");
module.exports = function(deployer) {
deployer.deploy(Adoption);
}
Compilation and migration (after launching Ganache 2.5.4):
x@x:~/Code/Blockchain/Truffle/pet-shop-tutorial$ truffle compile
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
x@x:~/Code/Blockchain/Truffle/pet-shop-tutorial$ truffle migrate
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
1_initial_migration.js
======================
Replacing 'Migrations'
----------------------
⠋ Blocks: 0 Seconds: 0 > transaction hash: 0x32545eb8f57d03e0a90b9fc14e7e193c936632be84401a7ecc1481294c766ef4
> Blocks: 0 Seconds: 0
> contract address: 0xC7801C9570FD9Dfb9dFca6f29461D2Dd3e8b813e
> block number: 1
> block timestamp: 1646245076
> account: 0xdFd735b7b2a4Cf28Bb7A85E2366FC73522573f8e
> balance: 99.99616114
> gas used: 191943 (0x2edc7)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00383886 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00383886 ETH
2_deploy_contracts.js
=====================
Replacing 'Adoption'
--------------------
⠋ Blocks: 0 Seconds: 0 > transaction hash: 0x17f512c4fdabc0b743b2b552352059275c64969f728ae23874d3fc162c9c4a27
> Blocks: 0 Seconds: 0
> contract address: 0x9E552555C766a1E1FdB5823FC3F0301C04C79651
> block number: 3
> block timestamp: 1646245078
> account: 0xdFd735b7b2a4Cf28Bb7A85E2366FC73522573f8e
> balance: 99.99123784
> gas used: 203827 (0x31c33)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00407654 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00407654 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.0079154 ETH
The top line in Ganache then says Tx count of 4, like the tutorial says, and some ETH is deducted from that address.
The problems arise at testing. I have copied and pasted the tests (both .Sol and .Js) to ensure no typos but they are compiling so I don't think that's it.
But here is the TestAdoption.sol code (in the test folder)"
pragma solidity ^0.5.0;
contract Adoption {
address[16] public adopters;
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 5);
adopters[petId] = msg.sender;
return petId;
}
function getAdopters() public view returns (address[16] memory) {
return adopters;
}
}
And the errors I get:
Using network 'development'.
Compiling your contracts...
===========================
> Compiling ./test/TestAdoption.sol
> Artifacts written to /tmp/test--7680-8QYwuQbGOkD1
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
TestAdoption
1) testUserCanAdoptPet
> No events were emitted
2) testGetAdopterAddressByPetId
> No events were emitted
3) testGetAdopterAddressByPetIdInArray
> No events were emitted
0 passing (37s)
3 failing
1) TestAdoption
testUserCanAdoptPet:
Error: Returned error: VM Exception while processing transaction: revert
at Context.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:91:1)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2) TestAdoption
testGetAdopterAddressByPetId:
Error: Owner of the expected pet should be this contract
at checkResultForFailure (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:65:1)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Context.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:91:1)
3) TestAdoption
testGetAdopterAddressByPetIdInArray:
Error: Owner of the expected pet should be this contract
at checkResultForFailure (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:65:1)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Context.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:91:1)
[Edit: I made errors copying the JS test, removed, if errors persist after getting it and the other error noted in the answer right I'll post seperately.]
Could anyone point me in the right direction to get these tests working? Thank you.
Your contract requires only pets with IDs between 0 and 5 can be adopted:
require(petId >= 0 && petId <= 5);
Yet on your test, you are adopting a pet with petId
of 8:
await adoption.adopt(8, { from: accounts[0] });
Try adopting a pet with petId
between 0 and 5 in your test.