Test upgrading a function

From: Sdk testing getting Invalid number of parameters

I understand this as you wanting to test an upgraded version of the contract.

To do this, you want to combine https://docs.openzeppelin.com/sdk/2.5/testing and
https://docs.openzeppelin.com/sdk/2.5/zos-lib

I have created some sample code based on the documentation above showing a version 0 and a version 1 of the contract and associated tests including upgrading from version 0 to version 1.

Let me know if you need more information.

Sample_v0.sol

pragma solidity ^0.5.0;

contract Sample_v0 {
  function greet() public pure returns (string memory) {
    return "A sample";
  }
}

Sample_v1.sol

pragma solidity ^0.5.0;

contract Sample_v1 {
  function greet() public pure returns (string memory) {
    return "An upgraded sample";
  }
}

Sample_v0.test.js

const { TestHelper } = require('@openzeppelin/cli');
const { Contracts, ZWeb3 } = require('@openzeppelin/upgrades');

ZWeb3.initialize(web3.currentProvider);

const Sample = Contracts.getFromLocal('Sample_v0');

require('chai').should();

contract('Sample_v0', function () {

  beforeEach(async function () {
    this.project = await TestHelper();
  })

  it('should create a proxy', async function () {
    const proxy = await this.project.createProxy(Sample);
    const result = await proxy.methods.greet().call();
    result.should.eq('A sample');
  })
})

Sample_v1.test.js

const { TestHelper } = require('@openzeppelin/cli');
const { Contracts, ZWeb3 } = require('@openzeppelin/upgrades');

ZWeb3.initialize(web3.currentProvider);

const Sample_v0 = Contracts.getFromLocal('Sample_v0');
const Sample_v1 = Contracts.getFromLocal('Sample_v1');

require('chai').should();

contract('Sample_v1', function () {

  beforeEach(async function () {
    this.project = await TestHelper();
  })

  it('should create a proxy', async function () {
    const proxy = await this.project.createProxy(Sample_v0);
    const result_v0 = await proxy.methods.greet().call();
    result_v0.should.eq('A sample');

    await this.project.upgradeProxy(proxy.address, Sample_v1);
    const result_v1 = await proxy.methods.greet().call();
    result_v1.should.eq('An upgraded sample');
   })
})
1 Like

Hi @CIX,

Was this what you were after?

Feel free to ask all the questions you need.

@abcoathup yes that’s perfect. I improved it a little to test Admin vs another random acccount. Thanks for your help. Love the SDK great Job.

1 Like