Updating tests to 3.2.2

This commit is contained in:
Richard Ramos 2018-10-09 19:00:32 -04:00
parent 2325e8507a
commit f4432fe26c
2 changed files with 12 additions and 26 deletions

View File

@ -3,7 +3,7 @@ Now that we finished coding our contract, we can proceed to build unit testing f
### We should be able to create a post and receive it via contract event
```
let receipt = await create(web3.utils.fromAscii(ipfsHash)).send();
let receipt = await DReddit.methods.create(web3.utils.fromAscii(ipfsHash)).send();
const event = receipt.events.NewPost;
postId = event.returnValues.postId;
assert.equal(web3.utils.toAscii(event.returnValues.description), ipfsHash);
@ -13,7 +13,7 @@ Now that we finished coding our contract, we can proceed to build unit testing f
### The post should have correct data
```
const post = await posts(postId).call();
const post = await DReddit.methods.posts(postId).call();
assert.equal(web3.utils.toAscii(post.description), ipfsHash);
assert.equal(post.owner, accounts[0]);
```
@ -22,7 +22,7 @@ Now that we finished coding our contract, we can proceed to build unit testing f
### We should't be able to vote twice for the same post
```
try {
const receipt = await vote(postId, 1).send();
const receipt = await DReddit.methods.vote(postId, 1).send();
assert.fail('should have reverted before');
} catch (error){
assert(error.message.search('revert') > -1, 'Revert should happen');

View File

@ -1,9 +1,6 @@
// our contract object to test
const DReddit = require('Embark/contracts/DReddit');
// contract methods we'll be testing
const {numPosts, create, vote, posts, canVote, getVote} = DReddit.methods;
const DReddit = embark.require('Embark/contracts/DReddit');
// variables that will be updated in the tests
let accounts;
@ -27,51 +24,40 @@ const ipfsHash = 'Qmc5gCcjYypU7y28oCALwfSvxCBskLuPKWpK4qpterKC7z';
// Embark exposes a global contract method as an alias
// for Mocha.describe
contract("DReddit contract", function () {
this.timeout(0);
it("should be able to create a post and receive it via contract event", async function () {
let receipt = await create(web3.utils.fromAscii(ipfsHash)).send();
const event = receipt.events.NewPost;
postId = event.returnValues.postId;
assert.equal(web3.utils.toAscii(event.returnValues.description), ipfsHash);
// TODO:
});
it("post should have correct data", async function (){
const post = await posts(postId).call();
assert.equal(web3.utils.toAscii(post.description), ipfsHash);
assert.equal(post.owner, accounts[0]);
// TODO:
});
it("one post should be registered", async function () {
const n = await numPosts().call();
const n = await DReddit.methods.numPosts().call();
assert.equal(n, 1);
});
it("should not be able to vote in an unexisting post", async function () {
const userCanVote = await canVote(123).call();
const userCanVote = await DReddit.methods.canVote("123").call();
assert.equal(userCanVote, false);
});
it("should be able to vote in a post if account hasn't voted before", async function () {
const userCanVote = await canVote(postId).call();
const userCanVote = await DReddit.methods.canVote(postId).call();
assert.equal(userCanVote, true);
});
it("should be able to vote in a post", async function () {
const receipt = await vote(postId, 1).send();
const receipt = await DReddit.methods.vote(postId, 1).send();
const Vote = receipt.events.Vote;
assert.equal(Vote.returnValues.voter, accounts[0]);
});
it("should't be able to vote twice", async function () {
try {
const receipt = await vote(postId, 1).send();
assert.fail('should have reverted before');
} catch (error){
assert(error.message.search('revert') > -1, 'Revert should happen');
}
// TODO:
});
});