correctly stub methods and test errors

This commit is contained in:
Matteo Pagliazzi
2016-03-20 16:06:31 +01:00
parent 657f19af0d
commit fea2e0d8c0
6 changed files with 60 additions and 40 deletions

View File

@@ -107,7 +107,18 @@ api.ops = {
sleep,
allocate,
};
api.fns = {};
import handleTwoHanded from './fns/handleTwoHanded';
import predictableRandom from './fns/predictableRandom';
import randomVal from './fns/randomVal';
import ultimateGear from './fns/ultimateGear';
api.fns = {
handleTwoHanded,
predictableRandom,
randomVal,
ultimateGear,
};
/*

View File

@@ -17,18 +17,20 @@ describe('shared.ops.allocate', () => {
it('throws an error if an invalid attribute is supplied', () => {
try {
expect(allocate(user, {
allocate(user, {
query: {stat: 'notValid'},
})).to.throw(BadRequest);
});
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.equal(i18n.t('invalidAttribute', {attr: 'notValid'}));
}
});
it('throws an error if the user doesn\'t have attribute points', () => {
try {
expect(allocate(user)).to.throw(NotAuthorized);
allocate(user);
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('notEnoughAttrPoints'));
}
});

View File

@@ -6,8 +6,7 @@ import {
} from '../../helpers/common.helper';
import count from '../../../common/script/count';
import buy from '../../../common/script/ops/buy';
import predictableRandom from '../../../common/script/fns/predictableRandom';
import randomVal from '../../../common/script/fns/randomVal';
import shared from '../../../common/script';
import content from '../../../common/script/content/index';
import {
NotAuthorized,
@@ -16,10 +15,6 @@ import i18n from '../../../common/script/i18n';
describe('shared.ops.buy', () => {
let user;
let fns = {
predictableRandom,
randomVal,
};
beforeEach(() => {
user = generateUser({
@@ -36,13 +31,13 @@ describe('shared.ops.buy', () => {
stats: { gp: 200 },
});
sinon.stub(fns, 'randomVal');
sinon.stub(fns, 'predictableRandom');
sinon.stub(shared.fns, 'randomVal');
sinon.stub(shared.fns, 'predictableRandom');
});
afterEach(() => {
fns.randomVal.restore();
fns.predictableRandom.restore();
shared.fns.randomVal.restore();
shared.fns.predictableRandom.restore();
});
context('Potion', () => {
@@ -69,8 +64,9 @@ describe('shared.ops.buy', () => {
user.stats.hp = 45;
user.stats.gp = 5;
try {
expect(buy(user, {params: {key: 'potion'}})).to.throw(NotAuthorized);
buy(user, {params: {key: 'potion'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
}
@@ -148,8 +144,9 @@ describe('shared.ops.buy', () => {
user.stats.gp = 20;
try {
expect(buy(user, {params: {key: 'armor_warrior_1'}})).to.throw(NotAuthorized);
buy(user, {params: {key: 'armor_warrior_1'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
}
@@ -181,12 +178,13 @@ describe('shared.ops.buy', () => {
context('failure conditions', () => {
it('does not open if user does not have enough gold', () => {
fns.predictableRandom.returns(YIELD_EQUIPMENT);
shared.fns.predictableRandom.returns(YIELD_EQUIPMENT);
user.stats.gp = 50;
try {
expect(buy(user, {params: {key: 'armoire'}})).to.throw(NotAuthorized);
buy(user, {params: {key: 'armoire'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
expect(user.items.gear.owned).to.eql({weapon_warrior_0: true});
expect(user.items.food).to.be.empty;
@@ -195,12 +193,13 @@ describe('shared.ops.buy', () => {
});
it('does not open without Ultimate Gear achievement', () => {
fns.predictableRandom.returns(YIELD_EQUIPMENT);
shared.fns.predictableRandom.returns(YIELD_EQUIPMENT);
user.achievements.ultimateGearSets = {healer: false, wizard: false, rogue: false, warrior: false};
try {
expect(buy(user, {params: {key: 'armoire'}})).to.throw(NotAuthorized);
buy(user, {params: {key: 'armoire'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('cannoyBuyItem'));
expect(user.items.gear.owned).to.eql({weapon_warrior_0: true});
expect(user.items.food).to.be.empty;
@@ -211,7 +210,7 @@ describe('shared.ops.buy', () => {
context('non-gear awards', () => {
it('gives Experience', () => {
fns.predictableRandom.returns(YIELD_EXP);
shared.fns.predictableRandom.returns(YIELD_EXP);
buy(user, {params: {key: 'armoire'}});
@@ -224,8 +223,8 @@ describe('shared.ops.buy', () => {
it('gives food', () => {
let honey = content.food.Honey;
fns.randomVal.returns(honey);
fns.predictableRandom.returns(YIELD_FOOD);
shared.fns.randomVal.returns(honey);
shared.fns.predictableRandom.returns(YIELD_FOOD);
buy(user, {params: {key: 'armoire'}});
@@ -236,7 +235,7 @@ describe('shared.ops.buy', () => {
});
it('does not give equipment if all equipment has been found', () => {
fns.predictableRandom.returns(YIELD_EQUIPMENT);
shared.fns.predictableRandom.returns(YIELD_EQUIPMENT);
user.items.gear.owned = fullArmoire;
user.stats.gp = 150;
@@ -256,12 +255,12 @@ describe('shared.ops.buy', () => {
beforeEach(() => {
let shield = content.gear.tree.shield.armoire.gladiatorShield;
fns.randomVal.returns(shield);
shared.fns.randomVal.returns(shield);
});
it('always drops equipment the first time', () => {
delete user.flags.armoireOpened;
fns.predictableRandom.returns(YIELD_EXP);
shared.fns.predictableRandom.returns(YIELD_EXP);
buy(user, {params: {key: 'armoire'}});
@@ -279,7 +278,7 @@ describe('shared.ops.buy', () => {
});
it('gives more equipment', () => {
fns.predictableRandom.returns(YIELD_EQUIPMENT);
shared.fns.predictableRandom.returns(YIELD_EQUIPMENT);
user.items.gear.owned = {
weapon_warrior_0: true,
head_armoire_hornedIronHelm: true,

View File

@@ -29,8 +29,9 @@ describe('shared.ops.buyMysterySet', () => {
context('failure conditions', () => {
it('does not grant mystery sets without Mystic Hourglasses', () => {
try {
expect(buyMysterySet(user, {params: {key: '201501'}})).to.throw(NotAuthorized);
buyMysterySet(user, {params: {key: '201501'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.eql(i18n.t('notEnoughHourglasses'));
expect(user.items.gear.owned).to.have.property('weapon_warrior_0', true);
}
@@ -47,8 +48,9 @@ describe('shared.ops.buyMysterySet', () => {
};
try {
expect(buyMysterySet(user, {params: {key: '301404'}})).to.throw(NotFound);
buyMysterySet(user, {params: {key: '301404'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotFound);
expect(err.message).to.eql(i18n.t('mysterySetNotFound'));
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
}

View File

@@ -31,12 +31,13 @@ describe('shared.ops.buyQuest', () => {
it('does not buy Quests without enough Gold', () => {
user.stats.gp = 1;
try {
expect(buyQuest(user, {
buyQuest(user, {
params: {
key: 'dilatoryDistress1',
},
})).to.throw(NotAuthorized);
});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
expect(user.items.quests).to.eql({});
expect(user.stats.gp).to.equal(1);
@@ -46,12 +47,13 @@ describe('shared.ops.buyQuest', () => {
it('does not buy nonexistent Quests', () => {
user.stats.gp = 9999;
try {
expect(buyQuest(user, {
buyQuest(user, {
params: {
key: 'snarfblatter',
},
})).to.throw(NotFound);
});
} catch (err) {
expect(err).to.be.an.instanceof(NotFound);
expect(err.message).to.equal(i18n.t('questNotFound', {key: 'snarfblatter'}));
expect(user.items.quests).to.eql({});
expect(user.stats.gp).to.equal(9999);
@@ -61,12 +63,13 @@ describe('shared.ops.buyQuest', () => {
it('does not buy Gem-premium Quests', () => {
user.stats.gp = 9999;
try {
expect(buyQuest(user, {
buyQuest(user, {
params: {
key: 'kraken',
},
})).to.throw(NotAuthorized);
});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('questNotGoldPurchasable', {key: 'kraken'}));
expect(user.items.quests).to.eql({});
expect(user.stats.gp).to.equal(9999);

View File

@@ -19,20 +19,22 @@ describe('shared.ops.buySpecialSpell', () => {
it('throws an error if params.key is missing', () => {
try {
expect(buySpecialSpell(user)).to.throw(BadRequest);
buySpecialSpell(user);
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.equal(i18n.t('missingKeyParam'));
}
});
it('throws an error if the spell doesn\'t exists', () => {
try {
expect(buySpecialSpell(user, {
buySpecialSpell(user, {
params: {
key: 'notExisting',
},
})).to.throw(NotFound);
});
} catch (err) {
expect(err).to.be.an.instanceof(NotFound);
expect(err.message).to.equal(i18n.t('spellNotFound', {spellId: 'notExisting'}));
}
});
@@ -40,12 +42,13 @@ describe('shared.ops.buySpecialSpell', () => {
it('throws an error if the user doesn\'t have enough gold', () => {
user.stats.gp = 1;
try {
expect(buySpecialSpell(user, {
buySpecialSpell(user, {
params: {
key: 'thankyou',
},
})).to.throw(NotAuthorized);
});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
}
});