Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. **/contracts/Migrations.sol**
  2.  
  3. ```solidity
  4. pragma solidity >=0.4.21 <0.6.0;
  5.  
  6. contract Migrations {
  7. address public owner;
  8. uint public last_completed_migration;
  9.  
  10. constructor() public {
  11. owner = msg.sender;
  12. }
  13.  
  14. modifier restricted() {
  15. if (msg.sender == owner) _;
  16. }
  17.  
  18. function setCompleted(uint completed) public restricted {
  19. last_completed_migration = completed;
  20. }
  21.  
  22. function upgrade(address new_address) public restricted {
  23. Migrations upgraded = Migrations(new_address);
  24. upgraded.setCompleted(last_completed_migration);
  25. }
  26. }
  27. ```
  28.  
  29. **/contracts/Sandbox.sol**
  30.  
  31. ```solidity
  32. pragma solidity ^0.5.0;
  33. contract Hello {
  34. int num = 0;
  35. function set(int newNum) external {
  36. num = newNum;
  37. }
  38. function get() external view returns (int) {
  39. return num;
  40. }
  41. }
  42. ```
  43.  
  44. **/migrations/1_initial_migration.js**
  45.  
  46. ```javascript
  47. const Migrations = artifacts.require("Migrations");
  48.  
  49. module.exports = function(deployer) {
  50. deployer.deploy(Migrations);
  51. };
  52. ```
  53.  
  54. **migrations/1_deploy_contracts.js**
  55.  
  56. ```javascriptconst Hello = artifacts.require("Hello");
  57.  
  58. module.exports = function(deployer) {
  59. deployer.deploy(Hello);
  60. };
  61. ```
  62.  
  63. **/test/Hello.js**
  64.  
  65. ```javascript
  66. const Hello = artifacts.require("./Hello.sol");
  67. contract("Hello", accounts => {
  68. it("unit test 1", async () => {
  69. const hello = await Hello.deployed();
  70. const actual = await hello.get.call();
  71. console.log("(unit test 1) The value is " + actual);
  72. });
  73. it("unit test 2", async () => {
  74. const hello = await Hello.deployed();
  75. await hello.set(5, {from : accounts[0]});
  76. console.log("(unit test 2) Setting value to 5.");
  77. });
  78. it("unit test 3", async () => {
  79. const hello = await Hello.deployed();
  80. const actual = await hello.get.call();
  81. console.log("(unit test 3) The value is " + actual);
  82. });
  83. });
  84. ```
  85.  
  86. And I've left **truffle-config.js** blank.
  87.  
  88. When I run `truffle develop` then `compile`, `migrate`, `test` the output is
  89.  
  90. ```
  91. Contract: Hello
  92. (unit test 1) The value is 0
  93. ✓ unit test 1
  94. (unit test 2) Setting value to 5.
  95. ✓ unit test 2
  96. (unit test 3) The value is 5
  97. ✓ unit test 3
  98. ```
  99.  
  100. Showing truffle persists state between unit tests.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement