Assertion error whilst running truffle test - unit-testing

I got an error while running truffle test on a smart contract, can anyone help me with this?
const KryptoBird = artifacts.require("KryptoBird");
// check for chai
require('chai')
.use(require('chai-as-promised'))
.should()
contract('KryptoBird', (accounts) => {
let contract
before( async () => {
contract = await KryptoBird.deployed()
})
describe('deployment', async() => {
it("deploys successfully", async () => {
const address = contract.address;
assert.notEqual(address, '')
assert.notEqual(address, null)
assert.notEqual(address, undefined)
assert.notEqual(address, 0x0)
})
it('has a name', async() => {
const name = await contract.name()
assert.equal(name, 'KryptoBird')
})
it('has a symbol', async() => {
const symbol = await contract.symbol()
assert.equal(symbol, 'KBIRDZ')
})
})
describe('minting', async ()=> {
it('creates a new token', async ()=> {
const result = await contract.mint('https...1')
const totalSupply = await contract.totalSupply()
assert.equal(totalSupply, 1)
const event = result.logs[0].args
assert.equal(event._from, '0x0000000000000000000000000000000000000000', 'from is the contract')
assert.equal(event._to, accounts[0], 'to is msg.sender')
await contract.mint('https...1').should.be.rejected
})
})
})
this is my code, and when I run it, it gives me an error that I don't understand
this is the error:
1 ) Contract : KryptoBird
minting
creates a new token :
AssertionError : expected promise to be rejected but it was fulfilled with { Object ( tx , receipt , ... ) }
error message
please help me, I've been stuck on this for a day.
heres my smart contract code, if u could help me
thanks a lot! :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import './ERC721Connecter.sol';
contract KryptoBird is ERC721Connecter {
string[] public kryptoBirdz;
mapping(string => bool) _kryptoBirdzExists;
function mint(string memory _kryptoBird) public {
require(!_kryptoBirdzExists[_kryptoBird],
'Error - kryptoBird already exists');
// this is deprecated - uint _id = KryptoBirdz.push(_kryptoBird);
kryptoBirdz.push(_kryptoBird);
uint _id = kryptoBirdz.length - 1;
// .push no logner returns the length but a ref to the added element
_mint(msg.sender, _id);
}
constructor() ERC721Connecter('KryptoBird','KBIRDZ')
{}
}

The problem is not you are not adding entry into the _kryptoBirdzExists mapping. before you mint, you are checking this condition
require(!_kryptoBirdzExists[_kryptoBird],
So after minting, you should be updating the mapping
_kryptoBirdzExists[_kryptoBird]=true;
Your test is not failing because you never added the 'https...1' to the mapping so require stament was passing so you were minting again

Related

Ethereum Hardhat Testing Expect Not Catching Revert Reason

I'm using Hardhat framewrok and doing some testing with an erc20 contract:
it("testing with second", async () => {
const amount = ethers.utils.parseEther("1");
await expect(
ERC20Contract.connect(second).transferFrom(
ERC20Contract.address,
second,
amount
)
).to.be.revertedWith("ERC20: insufficient allowance");
});
I'm expecting the contract to revert with an error of : ERC20: insufficient allowance, and this is the error I'm getting:
Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"name":"ProviderError","_stack":"ProviderError: HttpProviderError\n at HttpProvider.request (/home/kadiemq/hh-tut/erc20-tut/node_modules/hardhat/src/internal/core/providers/http.ts:78:19)\n at LocalAccountsProvider.request (/home/kadiemq/hh-tut/erc20-tut/node_modules/hardhat/src/internal/core/providers/accounts.ts:187:34)\n at processTicksAndRejections (node:internal/process/task_queues:96:5)\n at async EthersProviderWrapper.send (/home/kadiemq/hh-tut/erc20-tut/node_modules/#nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)","code":-32000,"_isProviderError":true,"data":{"stack":"RuntimeError: VM Exception while processing transaction: revert ERC20: insufficient allowance\n at Function.RuntimeError.fromResults (/tmp/.mount_ganachAQrIFW/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\n at module.exports (/tmp/.mount_ganachAQrIFW/resources/static/node/node_modules/ganache-core/lib/utils/gas/guestimation.js:142:32)","name":"RuntimeError"}}, tx={"data":"0x23b872dd000000000000000000000000624daf7e06c04e0ab541323b3d3e95b629745a6000000000000000000000000033757dfeda24de8dc2b46d348a035ad60bbc3a3f0000000000000000000000000000000000000000000000000de0b6b3a7640000","to":{},"from":"0x2c93fc47DC6aaF30CD5a6C47F59bD898842B0190","gasPrice":{"type":"BigNumber","hex":"0x04a817c800"},"type":0,"nonce":{},"gasLimit":{},"chainId":{}}, code=UNPREDICTABLE_GAS_LIMIT, version=abstract-signer/5.7.0)
in the error message I can see ERC20: insufficient allowance but the expect function is not getting it and failing the test.
Here is the contract:
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract OurToken is ERC20 {
constructor(uint256 initialSupply) ERC20("OurToken", "OT") {
_mint(address(this), initialSupply);
_approve(address(this), msg.sender, initialSupply);
}
}
Deploy:
import { DeployFunction } from "hardhat-deploy/dist/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import {
developmentChains,
INITIAL_SUPPLY,
networkConfig,
} from "../helper-hardhat-config";
const DeployToken: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { getNamedAccounts, deployments, network } = hre;
const { deploy, log } = deployments;
const { deployer } = await getNamedAccounts();
const chainId = network.config.chainId!;
const args = [INITIAL_SUPPLY];
const OurToken = await deploy("OurToken", {
from: deployer,
args: args,
log: true,
waitConfirmations: networkConfig[network.name].blockConfirmations || 1,
});
};
export default DeployToken;
DeployToken.tags = ["all", "token"];
Test:
import { deployments, ethers, getNamedAccounts } from "hardhat";
import { ERC20 } from "../typechain-types";
import { assert, expect } from "chai";
describe("Testing", async () => {
let deployer: string;
let second: string;
let ERC20Contract: ERC20;
beforeEach(async () => {
deployer = (await getNamedAccounts()).deployer;
second = (await getNamedAccounts()).second;
await deployments.fixture(["all"]);
});
it("testing with deployer", async () => {
ERC20Contract = await ethers.getContract("OurToken", deployer);
const amount = ethers.utils.parseEther("1");
const previousBalance = await ERC20Contract.balanceOf(deployer);
const tx = await ERC20Contract.transferFrom(
ERC20Contract.address,
deployer,
amount
);
tx.wait(1);
const newBalance = await ERC20Contract.balanceOf(deployer);
assert.equal(
previousBalance.add(amount).toString(),
newBalance.toString()
);
});
it("testing with second", async () => {
ERC20Contract = ERC20Contract.connect(second);
const amount = ethers.utils.parseEther("1");
await expect(
ERC20Contract.transferFrom(ERC20Contract.address, deployer, amount)
).to.be.revertedWith("ERC20: insufficient allowance");
});
});
So after almost a day of debugging it turns out that I need another plugin to use revertedWith() I need to install npm install --save-dev #nomiclabs/hardhat-waffle 'ethereum-waffle#^3.0.0' #nomiclabs/hardhat-ethers 'ethers#^5.0.0' and import it in the config file, otherwise I need to use rejectedWith("ERC20: insufficient allowance").
if anyone can explain this further it would be better because still I don't whats the difference between revertedWith and rejectedWith and why we need to install hardhat-waffle to be able to use revertedWith.
more info about hardhat-waffle in this page: https://hardhat.org/hardhat-runner/plugins/nomiclabs-hardhat-waffle

SyntaxError: Unexpected token o in JSON at position 1. In Solidity version ^0.8.7

Hey Guys, I'm using latest solidity version 0.8.7. I'm getting this SyntaxError: Unexpected token o in JSON at position 1. Please help me regarding this problem. When I run npm run test command, it gives me this error. I really need help to get rid of this issue. That's why I'm not able to proceed further in learning solidity. Thanks a lot
**Lottery.sol File - **
pragma solidity ^0.8.7; // latest solidity version...
contract Lottery
{
address public manager;
address[] public players;
constructor()
{
manager = msg.sender;
}
function enter() public payable
{
require(msg.value > .01 ether);
players.push(msg.sender);
}
function random() private view returns (uint256)
{
return
uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
}
function pickWinner() public restricted
{
require(msg.sender == manager);
uint256 index = random() % players.length;
uint256 amount = address(this).balance;
payable(players[index]).transfer(amount);
players = new address[](0);
}
modifier restricted()
{
require(msg.sender == manager);
_;
}
function getPlayers() public view returns (address[] memory)
{
return players;
}
}
Compile.js File -
const path = require("path");
const fs = require("fs");
const solc = require("solc");
const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(lotteryPath, "utf8");
var input = {
language: 'Solidity',
sources: {
'lottery.sol' : {
content: source
}
},
settings: {
outputSelection: {
'*': {
'*': [ '*' ]
}
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input))); // error on this line probably
const interface = output.contracts['lottery.sol'].lotteryPath.abi;
const bytecode = output.contracts['lottery.sol'].lotteryPath.evm.bytecode.object;
module.exports = {
interface,
bytecode,
};
lottery.test.js File
const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const web3 = new Web3(ganache.provider());
const { interface, bytecode } = require("../compile");
let lottery;
let accounts;
beforeEach(async () => {
accounts = await web3.eth.getAccounts();
lottery = await new web3.eth.Contract(JSON.parse(interface)) // error on this line probably
.deploy({ data: bytecode })
.send({ from: accounts[0], gas: '1000000' });
});
describe("Lottery Contract", () => {
it("deploys a contract", () => {
assert.ok(lottery.options.address);
});
});
deploy.js File -
const HDWalletProvider = require("#truffle/hdwallet-provider");
const Web3 = require("web3");
const { interface, bytecode } = require("./compile");
const provider = new HDWalletProvider(
"*******************************************",
"https://rinkeby.infura.io/v3/********************"
);
const web3 = new Web3(provider);
const deploy = async () => {
const accounts = await web3.eth.getAccounts();
console.log("Attempting to deploy from accoutns", accounts[0]);
const result = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode})
.send({ gas: '1000000', from: accounts[0] });
console.log("Contract deployed to", result.options.address);
};
Follow these steps.
Step 1: Check the solidity compiler version
Go to your package.json file. Under the dependencies object you will find the solc version.
Here make sure the solidity version is the same that you used in your Lottery.sol file.
If the version is not the same then please install the right version using npm
In my case, I am using ^0.8.8 in my solidity file. So I run the following command:
npm install solc#0.8.8
Step 2: Update your lottery.test.js file
In your lottery.test.js file update this line
lottery = await new web3.eth.Contract(JSON.parse(interface))
to this
lottery = await new web3.eth.Contract(interface)
Step 3: Update your deploy.js file
In your deploy.js file update this line
const result = await new web3.eth.Contract(JSON.parse(interface))
to this
const result = await new web3.eth.Contract(interface)
Also, it's a good practice to add your deployment code into a try/catch block like this:
try {
const accounts = await web3.eth.getAccounts();
console.log("Attempting to deploy from accoutns", accounts[0]);
const result = await new web3.eth.Contract(interface)
.deploy({ data: bytecode})
.send({ gas: '1000000', from: accounts[0] });
console.log("Contract deployed to", result.options.address);
} catch(error) {
console.log(error);
}
Note: Finally ensure in your deployment account you have enough ethers to test with. If you don't then get some ethers using faucets

TypeError: Cannot destructure property 'interface' of 'require(...)' as it is undefined

test.js file. I've tried installing/ uninstalling all different/previous versions of solidity but nothing is working properly. I am getting this TypeError: Cannot destructure property 'interface' of 'require(...)' as it is undefined. repeatedly. Please help me regarding this problem
const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const web3 = new Web3(ganache.provider());
const { interface, bytecode } = require('../compile');
let lottery;
let accounts;
beforeEach(async () => {
accounts = await web3.eth.getAccounts();
lottery = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode })
.send({ from: accounts[0], gas: "1000000" });
});
describe('Lottery Contract', () => {
it('deploys a contract', () => {
assert.ok(lottery.options.address);
});
});
compile.js file. Error while I try to run my compile.js file. Using command "npm run test". Please help me regarding this problem.
const path = require("path");
const fs = require("fs");
const solc = require("solc");
const lotteryPath = path.resolve(__dirname, "contracts", "Lottery.sol");
const source = fs.readFileSync(lotteryPath, "utf8");
// console.log(solc.compile(source, 1));
module.exports = solc.compile(source, 1).contracts[":Lottery"];
deploy.js file
const HDWalletProvider = require("#truffle/hdwallet-provider");
const Web3 = require("web3");
const { interface, bytecode } = require("./compile");
const provider = new HDWalletProvider(
"**************************************************",
"https://rinkeby.infura.io**************************"
);
const web3 = new Web3(provider);
const deploy = async () => {
const accounts = await web3.eth.getAccounts();
console.log("Attempting to deploy from accoutns", accounts[0]);
const result = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode})
.send({ gas: "1000000", from: accounts[0] });
console.log("Contract deployed to", result.options.address);
};
lottery.sol file
pragma solidity ^0.8.7; // latest solidity version...
contract Lottery
{
address public manager;
address[] public players;
constructor()
{
manager = msg.sender;
}
function enter() public payable
{
require(msg.value > .01 ether);
players.push(msg.sender);
}
function random() private view returns (uint256)
{
return
uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
}
function pickWinner() public restricted
{
require(msg.sender == manager);
uint256 index = random() % players.length;
uint256 amount = address(this).balance;
payable(players[index]).transfer(amount);
players = new address[](0);
}
modifier restricted()
{
require(msg.sender == manager);
_;
}
function getPlayers() public view returns (address[] memory)
{
return players;
}
}

sandbox.restore() won't reset stub's called count

I'm completely new to Sinon/Jest and unit testing, so I'm kinda lost here. I tried to make a sandbox to declare all my stubs inside it but even after using sandbox.restore() the stub's call count is preserved, so my test fails in the next 'it'.
I wasn't able to stub TypeORM's objects directly, so I decided to create fake objects with only the methods I needed and made TypeORM's getRepository() use my created objects.
I'm not sure if this approach is even correct but looks like my tests are working, I can assert the number of calls and it's parameters, in the second 'it' I can expect that the error thrown equals 'Email já cadastrado' and if I change the expected message the test fails.
The issue is that the number of calls won't reset before the next 'it' block. So I always get an error on the line "sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades)" since it's being called twice (once on previous 'it' and a second time on the current block).
If I remove that line I get an error in the assert transaction section, since I expect commitTransaction to not have been called but it was called once (in the previous 'it' block).
Image of my Error
Here is my test:
const sandbox = sinon.createSandbox();
// Simulating TypeORM's QueryRunner
const fakeQueryRunner = {
connect: sandbox.stub().returnsThis(),
startTransaction: sandbox.stub().returnsThis(),
rollbackTransaction: sandbox.stub().returnsThis(),
commitTransaction: sandbox.stub().returnsThis(),
release: sandbox.stub().returnsThis(),
manager: { save: sandbox.stub().returnsThis() },
};
// Simulating TypeORM's Connection
const fakeConnection = {
createQueryRunner: sandbox.stub().returns( fakeQueryRunner ),
getRepository: sandbox.stub().returnsThis(),
findOneOrFail: sandbox.stub().returnsThis(),
}
// I've hidden the mock of my parameters/entities (Usuarios, Senhas, Cidades) since I don't think it's needed to solve the problem.
describe('UserRepository', function () {
let userRepository;
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
UserRepository,
],
}).compile();
userRepository = module.get<UserRepository>(UserRepository);
});
describe('signUp', function () {
beforeEach(function () {
// typeORM.getConnection() returns my object simulating a Connection.
sandbox.stub(typeorm, 'getConnection').returns( fakeConnection as unknown as typeorm.Connection )
// Stubbing this.create()
sandbox.stub(userRepository, 'create').returns( Usuarios )
});
afterEach(function () {
sandbox.restore();
});
it('successfully signs up the user', async function () {
// Simulating sucessful save transaction
fakeQueryRunner.manager.save.onCall(0).resolves(Usuarios);
fakeQueryRunner.manager.save.onCall(1).resolves(Senhas);
// Calling my method
await userRepository.signUp(mockCredentialsDto);
// First interation, this line works
sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades);
// Asserting that transaction was commited
sandbox.assert.calledOnce(fakeQueryRunner.commitTransaction);
sandbox.assert.notCalled(fakeQueryRunner.rollbackTransaction);
});
it('throws a conflic exception as username already exists', async function () {
// Simulating a reject from transaction
fakeQueryRunner.manager.save.onCall(0).rejects({ code: '23505' });
// Calling my method and catching error
try {
await userRepository.signUp(mockCredentialsDto)
} catch (err) {
expect(err).toEqual(new Error('Email já cadastrado'));
}
// Second interation, this line giver ERROR (method called twice)
sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades);
// Asserting that transaction was rolled back, this also gives an error since commitTransaction was called once (in the first 'it' block)
sandbox.assert.notCalled(fakeQueryRunner.commitTransaction);
sandbox.assert.calledOnce(fakeQueryRunner.rollbackTransaction);
});
// I will make another describe block here eventually
});
Here is the method I'm testing:
async signUp(authCredentialsDto: AuthCredentialsDto): Promise<signUpMessage> {
const { nome, email, genero, dataNascimento, profissao, organizacao, atuacao, nomeCidade, uf, senha } = authCredentialsDto;
const connection = getConnection();
const user = this.create();
const senhas = new Senhas;
const cidade = await connection.getRepository(Cidades).findOneOrFail({where: { nome: nomeCidade, uf: uf } });
// Set values
user.nome = nome;
user.email = email;
user.genero = genero;
user.dataNascimento = dataNascimento;
user.profissao = profissao;
user.organizacao = organizacao;
user.atuacao = atuacao;
user.idCidade = cidade;
const salt = await bcrypt.genSalt();
senhas.senha = await this.hashPassword(senha, salt)
senhas.idUsuario2 = user;
// Make a transaction to save the data
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await queryRunner.manager.save(user);
await queryRunner.manager.save(senhas);
await queryRunner.commitTransaction();
} catch (error) {
if ( error.code === '23505' ) { // Usuário repetido
await queryRunner.rollbackTransaction();
throw new ConflictException('Email já cadastrado')
} else {
await queryRunner.rollbackTransaction();
throw new InternalServerErrorException;
}
} finally {
await queryRunner.release();
}
let success: signUpMessage;
return success;
}
Managed to make it work, I declared my Sinon Sandbox and Objects inside a beforeEach instead of the start of my test.
My test looks like this now:
describe('UserRepository', () => {
let userRepository
let sandbox
let fakeQueryRunner
let fakeConnection
let mockUser: Usuarios
beforeEach(async () => {
sandbox = sinon.createSandbox()
// Cria um objeto QueryRunner fake
fakeQueryRunner = {
connect: sandbox.stub().returnsThis(),
startTransaction: sandbox.stub().returnsThis(),
rollbackTransaction: sandbox.stub().returnsThis(),
commitTransaction: sandbox.stub().returnsThis(),
release: sandbox.stub().returnsThis(),
manager: { save: sandbox.stub().returnsThis() },
}
// Cria um objeto Connection fake (note que o stub de createQueryRunner retorna o objeto fakeQueryRunner )
fakeConnection = {
createQueryRunner: sandbox.stub().returns(fakeQueryRunner),
getRepository: sandbox.stub().returnsThis(),
findOneOrFail: sandbox.stub().returnsThis(),
}
mockUser = {
idUsuario: '1',
atuacao: 'lol',
dataNascimento: '10/10/2021',
email: 'teste#kik.com',
genero: Genero.MASCULINO,
nome: 'Teste Nome',
organizacao: 'Teste org',
profissao: 'Teste Prof',
idCidade: mockCidade,
membros: [],
senhas: mockSenha,
validatePassword: sandbox.stub().returnsThis(),
}
const module = await Test.createTestingModule({
providers: [UserRepository],
}).compile()
userRepository = module.get<UserRepository>(UserRepository)
})
afterEach(() => {
sandbox.restore()
})
describe('signUp', () => {
beforeEach(function () {
// Cria um método falso que retorna o objeto fakeConnection
sandbox.stub(typeorm, 'getConnection').returns((fakeConnection as unknown) as typeorm.Connection)
// Simula o Create de UserRepository
sandbox.stub(userRepository, 'create').returns(Usuarios)
})
it('successfully signs up the user', async () => {
// Salvar Usuário e Senha foi bem sucedido
fakeQueryRunner.manager.save.onCall(0).resolves(Usuarios)
fakeQueryRunner.manager.save.onCall(1).resolves(Senhas)
await userRepository.signUp(mockCredentialsDto)
// Verificando instanciação do repositório
const call1 = fakeConnection.getRepository.onCall(0).resolves(Cidades)
sandbox.assert.calledWith(call1, Cidades)
sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades)
// Verificando Transactions
sandbox.assert.calledOnce(fakeQueryRunner.commitTransaction)
sandbox.assert.notCalled(fakeQueryRunner.rollbackTransaction)
})
it('throws a conflic exception as username already exists', async () => {
// Houve um erro em um dos saves, resultando num rollback da transaction
fakeQueryRunner.manager.save.onCall(0).resolves(Usuarios)
fakeQueryRunner.manager.save.onCall(1).rejects({ code: '23505' })
try {
await userRepository.signUp(mockCredentialsDto)
} catch (err) {
expect(err).toEqual(new Error('Email já cadastrado'))
}
// Verificando instanciação do repositório
const call1 = fakeConnection.getRepository.onCall(0).resolves(Cidades)
sandbox.assert.calledWith(call1, Cidades)
sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades)
// Verificando Transactions
sandbox.assert.notCalled(fakeQueryRunner.commitTransaction)
sandbox.assert.calledOnce(fakeQueryRunner.rollbackTransaction)
})
})
})

Contract method invalid number of arguments

I just started playing around in Truffle and Solidity and wrote my first basic contract. I also wrote a test but it keeps failing giving me the following message:
Error: Invalid number of arguments to Solidity function
Now, the issue seems quite straight forward, I'm not pushing the right amount af arguments... Except as far as I can see I am.
This is my relevant contract code:
pragma solidity ^0.4.18;
contract FundEth {
mapping (uint => Project) _projects;
struct Project {
uint id;
uint targetWei;
uint targetBlock;
uint balanceWei;
string name;
string description;
bool payedOut;
}
function fund(uint projectId) public payable
{
_projects[projectId].balanceWei += msg.value;
}
function create(uint targetWei, uint blocks, string name, string description)
public
returns (uint)
{
Project memory p = Project({
id: ++_indexCounter,
targetWei: targetWei,
targetBlock: block.number + blocks,
balanceWei: 0,
name: name,
description: description,
payedOut: false
});
_projects[p.id] = p;
return p.id;
}
function getProjectName(uint projectId)
public
view
returns (string)
{
return "FOO";
}
function getProjectBalance(uint projectId)
public
view
returns (uint)
{
return 10000000;
}
...
}
And this is my test code:
const FundEth = artifacts.require("./FundEth.sol");
contract('FundEth', accounts => {
var _id;
var _fundEth;
it("should create a project", () => {
return FundEth.deployed()
.then(fundEth => {
_fundEth = fundEth;
return fundEth.create(1000000000000000000 /* 1 Eth */ , 5, "FOO", "We want to fund this for testing.")
}).then(id => {
_id = id;
return _fundEth.getProjectName.call(_id)
}).then(name => {
assert.equal(name, "FOO", "Has not created a valid project.");
});
});
it("should fund a project", () => {
return FundEth.deployed()
.then(fundEth => {
assert.notEqual(_id, 0);
_fundEth = fundEth;
_fundEth.fund.sendTransaction(_id, { from: accounts[0], value: 10000000 }); << SEEMS TO FAIL HERE.
}).then(() => {
return _fundEth.getProjectBalance.call(_id);;
}).then(balance => {
assert.equal(balance, 10000000, "Balance of test project was not 1 ether.");
});
});
});
I know the contract is not very useful right now, but I don't see why it fails. The full error:
1) Contract: FundEth
should fund a project:
Uncaught Error: Invalid number of arguments to Solidity function
at Object.InvalidNumberOfSolidityArgs (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:25:1)
at SolidityFunction.validateArgs (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:74:1)
at SolidityFunction.toPayload (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:90:1)
at SolidityFunction.sendTransaction (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:163:1)
at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-contract/contract.js:135:1
at new Promise (<anonymous>)
at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-contract/contract.js:126:1
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:118:7)
Changing the line to
return _fundEth.fund(_id, { from: accounts[0], value: 10000000 });
seems to fix the issue. However I also needed to remove the assert before the call in order to have a working test.