KitKarson
KitKarson

Reputation: 5637

Truffle test fails with TypeError

I have a very simple contract which works perfectly fine in remix editor.

I just wanted to learn truffle.I initiated an empty truffle project, placed the contract and get that compiled successfully.

however truffle test gives below error

Contract:

pragma solidity ^0.4.18;


contract Greetings{

    string public message;

    constructor() public {
        message = "Hello";
    }

    function getGreeting() public view returns (string){
        return message;
    }

}

Test:

var Greetings = artifacts.require("Greetings");


contract('Greetings Test', async (accounts) => {
    it("check for greetings message", async () => {
        let greeting = await Greetings.deployed();
        let message = await greeting.getGreeting().call();         
        console.log(message);
    });
});

Error:

 Contract: Greetings Test
    1) check for greetings message
    > No events were emitted


  0 passing (103ms)
  1 failing

  1) Contract: Greetings Test
       check for greetings message:
     TypeError: greeting.getGreeting(...).call is not a function
      at Context.it (test/campaignfactory.js:7:52)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Note: I would like to use asnyc/await.

Upvotes: 1

Views: 987

Answers (1)

qbsp
qbsp

Reputation: 312

There is an error on the way you invoke the function. Either you use

let message = await greeting.getGreeting.call(); 

or

let message = await greeting.getGreeting()

You can't mix the syntax. When you call the method (eg getGreeting()) web3 will check whether is a call or a transaction and will use the right one for you. doc

In case you'd like to be explicit then you should use the fist way.

Upvotes: 2

Related Questions