0

Here's my solidity:

contract Oracle {
  mapping(uint => uint) oracle_values;
  modifier onlyOwner {
    require(msg.sender == owner);
    _;
  }

  function Oracle() public {
    owner = msg.sender;
  }

  function StoreDocument(uint _key, uint _value) public onlyOwner() {
    oracle_values[_key] = _value;
  }

  function RetrieveData(uint _date) public constant returns (uint data) {
    return oracle_values[_date];
  }
}

and my simple Javascript test.

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

var startDate = 1;
var endDate = 2;
var startVal = 1000;
var endVal = 2000;

contract('Oracle',function(){

  it("Store Values", function(){
    return Oracle.deployed().then(function(instance){
      instance.StoreDocument(startDate,startVal);
      instance.StoreDocument(endDate,endVal);
    }).then(function(instance){
    assert.equal(instance.RetrieveData(startDate),startVal,"Start Value incorrect");
    assert.equal(instance.RetrieveData(endDate),endDate,"End Date incorrect");
    });
   });

I keep getting the error 'cannot read property of RetriveData'...which is odd since StoreDocument is found.

As you might be able to tell, it's kind of my first Truffle test, so any tips or even good resources would be much appreciated!

0

The problem is in the second function(instance). The Oracle.deployed() returns the instance, but that is only returned for (and is available to) the first then(). The return value for the second then() is undefined, since it's looking for the return value from the code from within the first then.

You need to save the instance to make it accessible for both thens, for example:

contract('Oracle',function(){

  let contractInstance;

  it("Store Values", function(){
    return Oracle.deployed().then(function(instance){

      contractInstance = instance; // save here to make it accessible

      contractInstance.StoreDocument(startDate,startVal);
      contractInstance.StoreDocument(endDate,endVal);

    }).then(function(){ // there is no instance available here

    // change the asserts to refer to the saved instance:
    assert.equal(contractInstance.RetrieveData(startDate),startVal,"Start Value incorrect");
    assert.equal(contractInstance.RetrieveData(endDate),endDate,"End Date incorrect");
    });
   });
  • Thanks for the help! it seems that the RetrieveData is now just returning an empty Promise ({}). I separated out the assert and retrieve data steps also and that doesn't help – thefett Dec 21 '17 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.