在松露测试期间不是合约地址
is not contract address during truffle test
我有一个像这样的愚蠢的智能合约:
pragma solidity ^0.4.24;
contract ProdottoFactory {
function foo() view returns(string nome){
return "foo";
}
}
我想用 chai 测试一下
var Prodotto = artifacts.require("ProdottoFactory");
expect = require("chai").expect;
contract("descrizione primo test", function () {
describe("test 2", function () {
it("blablabla", function () {
return Prodotto.new().then(
istance => {
prodottoContract = istance;
}
)
})
})
})
contract("descrizione primo test2", function () {
describe("test 2 2", function () {
it("blablabla2",function () {
return prodottoContract.foo().then(function (res) {
expect(res.toString()).to.be.equal("foo")
})
})
})
})
当我运行命令
松露测试
我有这个错误
Error: Attempting to run transaction which calls a contract function, but recipient address 0xe8f29e5c4ca41c5b40ed989439ddeae4d9384984 is not a contract address
truffle.js
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545, // Ganache GUI
network_id: "*" // Match any network id
}
}
};
contracts/ProdottoFactory.sol
pragma solidity ^0.4.24;
contract ProdottoFactory {
function foo() pure public returns(string nome){
return "foo";
}
}
test/ProdottoFactory.js
var pf = artifacts.require("ProdottoFactory");
contract('ProdottoFactory', function(accounts) {
var pfInstance;
before(function() {
return pf.new()
.then(function(instance) {
pfInstance = instance;
});
});
it("should return foo", function() {
return pfInstance.foo.call()
.then(function(str) {
assert.equal(str, "foo");
});
});
});
我对你的合同做了 2 处小改动:
我添加了 public
关键字。始终定义函数的可见性是一种很好的做法。
我把view
换成了pure
。当您不从 blockchain/state 变量读取时,请使用 pure
。更多信息可以在文档中找到 here.
仅供参考,您不需要 chai 或 mocha 库。当您使用 truffle init
命令初始化 Truffle 项目时,它已经存在了。 before
关键字是 Mocha 库的一部分。您可以阅读更多相关信息 here.
最后,如果您想了解 Truffle 中 new
和 deployed
关键字之间的区别,请阅读我的帖子 here。
我有一个像这样的愚蠢的智能合约:
pragma solidity ^0.4.24;
contract ProdottoFactory {
function foo() view returns(string nome){
return "foo";
}
}
我想用 chai 测试一下
var Prodotto = artifacts.require("ProdottoFactory");
expect = require("chai").expect;
contract("descrizione primo test", function () {
describe("test 2", function () {
it("blablabla", function () {
return Prodotto.new().then(
istance => {
prodottoContract = istance;
}
)
})
})
})
contract("descrizione primo test2", function () {
describe("test 2 2", function () {
it("blablabla2",function () {
return prodottoContract.foo().then(function (res) {
expect(res.toString()).to.be.equal("foo")
})
})
})
})
当我运行命令 松露测试 我有这个错误
Error: Attempting to run transaction which calls a contract function, but recipient address 0xe8f29e5c4ca41c5b40ed989439ddeae4d9384984 is not a contract address
truffle.js
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545, // Ganache GUI
network_id: "*" // Match any network id
}
}
};
contracts/ProdottoFactory.sol
pragma solidity ^0.4.24;
contract ProdottoFactory {
function foo() pure public returns(string nome){
return "foo";
}
}
test/ProdottoFactory.js
var pf = artifacts.require("ProdottoFactory");
contract('ProdottoFactory', function(accounts) {
var pfInstance;
before(function() {
return pf.new()
.then(function(instance) {
pfInstance = instance;
});
});
it("should return foo", function() {
return pfInstance.foo.call()
.then(function(str) {
assert.equal(str, "foo");
});
});
});
我对你的合同做了 2 处小改动:
我添加了
public
关键字。始终定义函数的可见性是一种很好的做法。我把
view
换成了pure
。当您不从 blockchain/state 变量读取时,请使用pure
。更多信息可以在文档中找到 here.
仅供参考,您不需要 chai 或 mocha 库。当您使用 truffle init
命令初始化 Truffle 项目时,它已经存在了。 before
关键字是 Mocha 库的一部分。您可以阅读更多相关信息 here.
最后,如果您想了解 Truffle 中 new
和 deployed
关键字之间的区别,请阅读我的帖子 here。