0

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([])
  })
})```

1 Answer 1

0

Note: createCharacter always returns the first character. As for the error messsage, It seems to be comparing the result to an object where as it should be a class instance type Character which is apparently different. Try this const {...object} = classInstance to convert to type object.

class Character {
  constructor(characterName, characterAge) {
    this.characterName = characterName
    this.characterAge = characterAge
    this.health = 100
    this.invintory = []
    this.weapons = []
  }
}

var instance = new Character("Jack", 80)
var { ...obj } = instance;

// but they do look the same
console.log(instance)
console.log(obj)

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the advice. i made the last line of createCharacter return {...character} and it seems to pass the tests...im not sure why. Whats that called so i can read up on it?
spread syntax and de-structuring (stackoverflow.com/a/66774294/3807365)
You don't need to return {...character} from createCharacter because it will convert to object. You want to do the conversion only for testing purposes.
is this right. it seems to work createCharacter(characterName, characterAge) { const { ...obj } = new Character(characterName, characterAge) this.characters.push({ ...obj }) console.log('char = ', { ...obj }) console.log('characters array = ', this.characters) return { ...obj } }
You can think of {...obj} = instance as cloning the object. Once you cloned the instance to obj (why do you?) you can continue with using obj no need to clone again and again
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.