传递一个mocha-before中生成的值给express app
Pass a value generated in Mocha-before to express app
我正在使用 express 和 mocha 进行测试。
从 Express Im 从部署在 ropsten 中的智能合约中获取信息。所以地址总是一样的。我在配置文件中有那个地址。
对于本地测试,我想在每次测试前使用testrpc并部署智能合约。所以当我需要将 deploy 的地址传递给 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 钩子中,你应该导入这个变量,并为它设置地址。
在你的应用程序中,你应该瞄准这个变量
我正在使用 express 和 mocha 进行测试。 从 Express Im 从部署在 ropsten 中的智能合约中获取信息。所以地址总是一样的。我在配置文件中有那个地址。
对于本地测试,我想在每次测试前使用testrpc并部署智能合约。所以当我需要将 deploy 的地址传递给 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 钩子中,你应该导入这个变量,并为它设置地址。 在你的应用程序中,你应该瞄准这个变量