I had the same question, and I had to look around a while, but I came across a great example HERE that helped me with more detail to wtk219's excellent answer.
This link tests all functions and events for ERC20 compliant contracts. In the context of testing approve, allowance and transfer from ERC20 compliant functions, you come across the case where you can control which address is "using" the deployed contract function.
The following is that particular Mocha and Chai style testing where accounts[1]
is actually using the smart contract in the test.
The main line you care about in the code below is:
return token.transferFrom(accounts[0], accounts[2], 200000, {from: accounts[1]});
Here is the code from the link above:
it("should give accounts[1] authority to spend account[0]'s token", function() {
var token;
return Token.deployed().then(function(instance){
token = instance;
return token.approve(accounts[1], 200000);
}).then(function(){
return token.allowance.call(accounts[0], accounts[1]);
}).then(function(result){
assert.equal(result.toNumber(), 200000, 'allowance is wrong');
return token.transferFrom(accounts[0], accounts[2], 200000, {from: accounts[1]});
}).then(function(){
return token.balanceOf.call(accounts[0]);
}).then(function(result){
assert.equal(result.toNumber(), 300000, 'accounts[0] balance is wrong');
return token.balanceOf.call(accounts[1]);
}).then(function(result){
assert.equal(result.toNumber(), 500000, 'accounts[1] balance is wrong');
return token.balanceOf.call(accounts[2]);
}).then(function(result){
assert.equal(result.toNumber(), 200000, 'accounts[2] balance is wrong');
})
});
I used this test with few modifications for my specific numbers, and it worked flawlessly.