My program is returning the wrong kind of object. My console.logs always seem to find what i create but im either returning it wrong or my tests are set up wrong. Could anyone help me?
So im building a very basic multi page multi class program. styled like a game. Use OOP and TDD. All my test fail and claim i am returning the wrong type of object. for example
Expected object to be a kind of Object, but was Character({ characterName: 'Dr Alan Grant', characterAge: 42, health: 100, invintory: [ ], weapons: [ ] }).
or TypeError: Cannot read properties of undefined (reading '0')
It seems to be the same error so it must really be one simple thing im not quite getting about building using classes and multiple classes. I have tried googling it but had very little luck. I know the code is a little long. But its the same issue throughtout so if you fix one you fix them all i guess.
onst Character = require('../src/character')
class Game {
constructor() {
this.characters = []
}
createCharacter(characterName, characterAge) {
const character = new Character(characterName, characterAge)
this.characters.push(character)
console.log('char = ', character)
return this.characters[0]
}
findCharacter(searchName) {
for (let i = 0; i < this.characters.length; i++) {
/// which way to i look character or characters
if (this.characters[i].characterName === searchName) {
return this.characters[i]
} else {
return 'Error! Character not found!'
}
}
}
showAllCharacters() {
return this.characters
}
deleteCharacter(searchName) {
for (let i = 0; i < this.characters.length; i++) {
if (this.character[i].characterName === searchName) {
this.characters.splice(this.characters[i], 1)
return 'Player deleted'
} else {
return 'Player Not Found'
}
}
}
}
module.exports = Game
class Character {
constructor(characterName, characterAge) {
this.characterName = characterName
this.characterAge = characterAge
this.health = 100
this.invintory = []
this.weapons = []
}
}
module.exports = Character
const Character = require('../src/character')
// const Attackers = require('../src/attackers')
const Game = require('../src/Game')
describe('Jurrasic World ', () => {
let character
beforeEach(function () {
character = new Character()
})
// let attackers
// beforeEach(function () {
// attackers = new Attackers()
// })
let game
beforeEach(function () {
game = new Game()
})
it('create a new character', () => {
// set up
const expectedResult = {
characterName: 'Dr Alan Grant',
characterAge: 42,
health: 100,
invintory: [],
weapons: []
}
// execute
const result = game.createCharacter('Dr Alan Grant', 42)
// verifty
expect(result).toEqual(expectedResult)
})
it('delete character by name', () => {
// set up
const expectedResult = 'Player deleted'
// execute
game.createCharacter('Dr Alan Grant', 42)
const result = game.deleteCharacter('Dr Alan Grant')
// verify
expect(result).toEqual(expectedResult)
// expect(game.characters).toEqual([])
})
})```