Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **/contracts/Migrations.sol**
- ```solidity
- pragma solidity >=0.4.21 <0.6.0;
- contract Migrations {
- address public owner;
- uint public last_completed_migration;
- constructor() public {
- owner = msg.sender;
- }
- modifier restricted() {
- if (msg.sender == owner) _;
- }
- function setCompleted(uint completed) public restricted {
- last_completed_migration = completed;
- }
- function upgrade(address new_address) public restricted {
- Migrations upgraded = Migrations(new_address);
- upgraded.setCompleted(last_completed_migration);
- }
- }
- ```
- **/contracts/Sandbox.sol**
- ```solidity
- pragma solidity ^0.5.0;
- contract Hello {
- int num = 0;
- function set(int newNum) external {
- num = newNum;
- }
- function get() external view returns (int) {
- return num;
- }
- }
- ```
- **/migrations/1_initial_migration.js**
- ```javascript
- const Migrations = artifacts.require("Migrations");
- module.exports = function(deployer) {
- deployer.deploy(Migrations);
- };
- ```
- **migrations/1_deploy_contracts.js**
- ```javascriptconst Hello = artifacts.require("Hello");
- module.exports = function(deployer) {
- deployer.deploy(Hello);
- };
- ```
- **/test/Hello.js**
- ```javascript
- const Hello = artifacts.require("./Hello.sol");
- contract("Hello", accounts => {
- it("unit test 1", async () => {
- const hello = await Hello.deployed();
- const actual = await hello.get.call();
- console.log("(unit test 1) The value is " + actual);
- });
- it("unit test 2", async () => {
- const hello = await Hello.deployed();
- await hello.set(5, {from : accounts[0]});
- console.log("(unit test 2) Setting value to 5.");
- });
- it("unit test 3", async () => {
- const hello = await Hello.deployed();
- const actual = await hello.get.call();
- console.log("(unit test 3) The value is " + actual);
- });
- });
- ```
- And I've left **truffle-config.js** blank.
- When I run `truffle develop` then `compile`, `migrate`, `test` the output is
- ```
- Contract: Hello
- (unit test 1) The value is 0
- ✓ unit test 1
- (unit test 2) Setting value to 5.
- ✓ unit test 2
- (unit test 3) The value is 5
- ✓ unit test 3
- ```
- Showing truffle persists state between unit tests.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement