Initializable.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { assert } = require('chai');
  3. const InitializableMock = artifacts.require('InitializableMock');
  4. const ConstructorInitializableMock = artifacts.require('ConstructorInitializableMock');
  5. const SampleChild = artifacts.require('SampleChild');
  6. contract('Initializable', function (accounts) {
  7. describe('basic testing without inheritance', function () {
  8. beforeEach('deploying', async function () {
  9. this.contract = await InitializableMock.new();
  10. });
  11. context('before initialize', function () {
  12. it('initializer has not run', async function () {
  13. assert.isFalse(await this.contract.initializerRan());
  14. });
  15. });
  16. context('after initialize', function () {
  17. beforeEach('initializing', async function () {
  18. await this.contract.initialize();
  19. });
  20. it('initializer has run', async function () {
  21. assert.isTrue(await this.contract.initializerRan());
  22. });
  23. it('initializer does not run again', async function () {
  24. await expectRevert(this.contract.initialize(), 'Initializable: contract is already initialized');
  25. });
  26. });
  27. context('nested under an initializer', function () {
  28. it('initializer modifier reverts', async function () {
  29. await expectRevert(this.contract.initializerNested(), 'Initializable: contract is already initialized');
  30. });
  31. it('onlyInitializing modifier succeeds', async function () {
  32. await this.contract.onlyInitializingNested();
  33. assert.isTrue(await this.contract.onlyInitializingRan());
  34. });
  35. });
  36. it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
  37. await expectRevert(this.contract.initializeOnlyInitializing(), 'Initializable: contract is not initializing');
  38. });
  39. });
  40. it('nested initializer can run during construction', async function () {
  41. const contract2 = await ConstructorInitializableMock.new();
  42. assert.isTrue(await contract2.initializerRan());
  43. assert.isTrue(await contract2.onlyInitializingRan());
  44. });
  45. describe('complex testing with inheritance', function () {
  46. const mother = 12;
  47. const gramps = '56';
  48. const father = 34;
  49. const child = 78;
  50. beforeEach('deploying', async function () {
  51. this.contract = await SampleChild.new();
  52. });
  53. beforeEach('initializing', async function () {
  54. await this.contract.initialize(mother, gramps, father, child);
  55. });
  56. it('initializes human', async function () {
  57. assert.equal(await this.contract.isHuman(), true);
  58. });
  59. it('initializes mother', async function () {
  60. assert.equal(await this.contract.mother(), mother);
  61. });
  62. it('initializes gramps', async function () {
  63. assert.equal(await this.contract.gramps(), gramps);
  64. });
  65. it('initializes father', async function () {
  66. assert.equal(await this.contract.father(), father);
  67. });
  68. it('initializes child', async function () {
  69. assert.equal(await this.contract.child(), child);
  70. });
  71. });
  72. });