Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Unit Testing Behavior Driven Development with Mocha & Chai Writing Our First Test Suite - Part 2

Failed npm test result

When I run npm test after defining test spec for "should correctly report a ship located at the given coordinate" "should correctly handle ships located at more than one coordinate" i keep getting AssertationError: "expected false to be true"

Please, what am I doing wrong? below is my test script.

ship_test.js

var expect = require("chai").expect;
describe("checkForShip", function() {
  var checkForShip = require("../game_logic/ship_methods").checkForShip;
  it("should correctly report no ship at a given players coordinate", function() {
    player = {
      ships: [
        {
          locations: [[0, 0]]
        }
      ]
    };
    expect(checkForShip(player, [9, 9])).to.be.false;
  });
  it("should correctly report a ship located at the  given coordinate", function() {
    player = {
      ships: [
        {
          locations: [[0, 0]]
        }
      ]
    };
    expect(checkForShip(player, [0, 0])).to.be.true;
  });
  it("should correctly handle  ships located at more than  one coordinate", function() {
    player = {
      ships: [
        {
          locations: [[0, 0], [0, 1]]
        }
      ]
    };
    expect(checkForShip(player, [0, 1])).to.be.true;
    expect(checkForShip(player, [0, 0])).to.be.true;
    expect(checkForShip(player, [9, 9])).to.be.false;
  });
}); 

ship_methods.js

function checkForShip(player, coordinates) {
  var shipPresent, ship;
  for (var i = 0; i < player.ships.length; i++) {
    ship = player.ships[i];
    shipPresent = ship.locations.filter(function(actualCoordinate) {
      return (
        actualCoordinate[0] === [0] && actualCoordinate[1] === coordinates[1]
      );
    })[0];
    if (!shipPresent) {
      return false;
    } else {
      return true;
    }
  }
}
module.exports.checkForShip = checkForShip;

1 Answer

I have exactly the same issue:

function checkForShip(player, coordinates){
    var shipPresent, ship;

    for(var i = 0; i < player.ships.length; i++){
        ship = player.ships[i];

        shipPresent = ship.locations.filter(function (actualCoordinate){
            return (actualCoordinate[0]=== coordinates[0]) && (actualCoordinate[0]=== coordinates[1])})[0];

        if (shipPresent){
            return true;
        }
    }

    return false;
}

module.exports.checkForShip = checkForShip;
var expect = require('chai').expect;

describe('checkForShip', function () {
    var checkForShip = require('../game_logic/ship_methods').checkForShip;

    it('should correctly report no ship at a given players coordinate', function () {

        player = {
            ships: [{
                locations: [
                    [0, 0]
                ]
            }]
        };

        expect(checkForShip(player, [9, 9])).to.be.false;
    });

    it('should correctly report a ship at the given players coordinates', function () {

        player = {
            ships: [{
                locations: [
                    [0, 0]
                ]
            }]
        };

        expect(checkForShip(player, [0, 0])).to.be.true;
    });

    it('should handle ships at more than one coordinate', function () {

        player = {
            ships: [{
                locations: [
                    [0, 0],
                    [0, 1]
                ]
            }]
        };

        expect(checkForShip(player, [0, 1])).to.be.true;
        expect(checkForShip(player, [0, 0])).to.be.true;
        expect(checkForShip(player, [9, 9])).to.be.false;
    });

    it('should handle multiple ships', function () {

        player = {
            ships: [{
                    locations: [
                        [0, 0],
                        [0, 1]
                    ]
                },
                {
                    locations: [
                        [1, 0],
                        [1, 1]
                    ]
                },
                {
                    locations: [
                        [2, 0],
                        [2, 1][2, 2],
                        [2, 3]
                    ]
                }
            ]
        };
        expect(checkForShip(player, [0, 0])).to.be.true;
        expect(checkForShip(player, [0, 1])).to.be.true;
        expect(checkForShip(player, [1, 0])).to.be.true;
        expect(checkForShip(player, [1, 1])).to.be.true;
        expect(checkForShip(player, [2, 0])).to.be.true;
        expect(checkForShip(player, [2, 1])).to.be.true;
        expect(checkForShip(player, [2, 2])).to.be.true;
        expect(checkForShip(player, [2, 3])).to.be.true;
        expect(checkForShip(player, [9, 9])).to.be.false;
    });
});

I continued the lesson to see if it would cover this.. but it didn't.