我正在使用express和mocha进行测试。来自Express Im从ropsten部署的智能合约中获取信息。所以地址总是一样的。我在配置文件中有该地址。
对于本地测试,我想在每次测试之前使用testrpc并部署智能合约。所以当我需要将部署的地址传递给Express应用程序时。
我的代码是:
Test.js
beforeEach(async function () {
index = DeployContract() //this returns a random address
server = await app.listen(3000)
})
在应用程序中
const CONFIG = require('../config.json')
const contex = {
indexAddress: CONFIG.indexAddress, // or .env
gasMargin: CONFIG.gasMargin,
web3: web3
}
router.get('/manager', (req, res, next) => {
const manager = new Manager(contex) //this must be the address returned beforeEach
// do something
res.send(200)
})
我需要使用生成的地址为indexAddress: CONFIG.indexAddress
,所以我可以在构造函数中使用contex。
我想你应该引用一个全局变量来保存DeployContract()生成的地址,例如:
档案:合同地址
let address = ""
export default address
然后在您的beforeEach挂钩中,您应该导入此变量,并将地址设置为它。在你的应用程序中,你应该针对这个变量