Initializable.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { assert } = require('chai');
  3. const InitializableMock = artifacts.require('InitializableMock');
  4. const SampleChild = artifacts.require('SampleChild');
  5. contract('Initializable', function (accounts) {
  6. describe('basic testing without inheritance', function () {
  7. beforeEach('deploying', async function () {
  8. this.contract = await InitializableMock.new();
  9. });
  10. context('before initialize', function () {
  11. it('initializer has not run', async function () {
  12. assert.isFalse(await this.contract.initializerRan());
  13. });
  14. });
  15. context('after initialize', function () {
  16. beforeEach('initializing', async function () {
  17. await this.contract.initialize();
  18. });
  19. it('initializer has run', async function () {
  20. assert.isTrue(await this.contract.initializerRan());
  21. });
  22. it('initializer does not run again', async function () {
  23. await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
  24. });
  25. });
  26. context('after nested initialize', function () {
  27. beforeEach('initializing', async function () {
  28. await this.contract.initializeNested();
  29. });
  30. it('initializer has run', async function () {
  31. assert.isTrue(await this.contract.initializerRan());
  32. });
  33. });
  34. });
  35. describe('complex testing with inheritance', function () {
  36. const mother = 12;
  37. const gramps = '56';
  38. const father = 34;
  39. const child = 78;
  40. beforeEach('deploying', async function () {
  41. this.contract = await SampleChild.new();
  42. });
  43. beforeEach('initializing', async function () {
  44. await this.contract.initialize(mother, gramps, father, child);
  45. });
  46. it('initializes human', async function () {
  47. assert.equal(await this.contract.isHuman(), true);
  48. });
  49. it('initializes mother', async function () {
  50. assert.equal(await this.contract.mother(), mother);
  51. });
  52. it('initializes gramps', async function () {
  53. assert.equal(await this.contract.gramps(), gramps);
  54. });
  55. it('initializes father', async function () {
  56. assert.equal(await this.contract.father(), father);
  57. });
  58. it('initializes child', async function () {
  59. assert.equal(await this.contract.child(), child);
  60. });
  61. });
  62. });