2016-04-14 81 views
8

Ho una funzione del contratto che emette eventi su ogni chiamata.Test ethereum Log di eventi con tartufo

Vorrei avere un evento emesso su ogni test che sono di passaggio, qui alcuni test:

it("should emit Error event when sending 5 ether", function(done){ 
    var insurance = CarInsurance.deployed(); 

    insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(done).catch(done); 
}); 

it("should emit Error event when sending 5 ether", function(done){ 
    var insurance = CarInsurance.deployed(); 

    insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function(txHash){ 
    assert.notEqual(txHash, null); 
    }).then(done).catch(done); 
}); 

it("should emit Error event when sending 5 ether", function(done){ 
    var insurance = CarInsurance.deployed(); 

    insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function(done){ 
    done(); 
    }).catch(done); 
}); 

I risultati sono:

1) should emit Error event when sending 5 ether 

Events emitted during test: 
--------------------------- 

Error(error: Must send 10 ether) 

--------------------------- 
✓ should emit Error event when sending 5 ether (11120ms) 
✓ should emit Error event when sending 5 ether (16077ms) 


3 passing (51s) 
1 failing 

1) Contract: CarInsurance should emit Error event when sending 5 ether: 
Error: done() invoked with non-Error: 0x87ae32b8d9f8f09dbb5d7b36267370f19d2bda90d3cf7608629cd5ec17658e9b 

Si può vedere che l'unico che è registrato fallire.

Qualche idea?

Grazie

risposta

9

Si passa tx hash in funzione di fatto(). Credo che il problema è in linea:

insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(done).catch(done); 

modificarla in:

insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function() { done(); }).catch(done); 

per verificare gli eventi:

it("should check events", function(done) { 
    var watcher = contract.Reward(); 

    // we'll send rewards 
    contract.sendReward(1, 10000, {from: accounts[0]}).then(function() { 
    return watcher.get(); 
    }).then(function(events) { 
    // now we'll check that the events are correct 
    assert.equal(events.length, 1); 
    assert.equal(events[0].args.beneficiary.valueOf(), 1); 
    assert.equal(events[0].args.value.valueOf(), 10000); 
    }).then(done).catch(done); 
}); 
+0

Infatti, solo il primo test emette l'evento "Errore" che desidero. Quando cambio come hai detto tu non emette nulla, sembra che aspetti() ... – ltheron

+0

@ user3262670 Non vedo alcun controllo per gli eventi in prova. + tutti i casi di test sono denominati "dovrebbero emettere un evento di errore quando si invia 5 etere", quindi non si può dire quale di essi non va a buon fine. – Aldekein

+0

Possiamo controllare gli eventi in prova? Volevo mostrare tre modi per fare lo stesso test case. Voglio solo mostrare gli eventi durante i test e l'unico modo per farlo è quando il test fallisce ... – ltheron

0

C'è un aiutante di fare proprio questo:

npm install --save truffle-test-utils 

Nella parte superiore del test:

require('truffle-test-utils').init(); 

Nel tuo test:

let result = await insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}); 
assert.web3Event(result, { 
    event: 'Error', 
    args: { 
    error: 'Must send 10 ether' 
    } 
}, 'Error event when sending 5 ether'); 

Full disclosure: Sono l'autore di questo pacchetto. L'ho scritto dopo aver cercato tale soluzione su SO, ma non l'ho trovato.