mercredi 25 mai 2016

JavaScript Jasmine Test Failing

I have this specific JavaScript code

function hideGreenCarsFromView(greenCar) {
                    if (greenCar) {
                        var car = parkingCarTools.getContainerOfgreenCar(greenCar, sr.pageModel.garageData.cars);
                        if (car && car.greenCars && car.greenCars.length > 0) {
                            parkingCarTools.removegreenCarFromgreenCarsList(greenCar, car.greenCars);
                        }
                    }

                    if (car.greenCars && car.greenCars.length == 0) {
                        sr.showgreenCarsHeader = false;
                    }
                };

In this code snippet I am removing the green cars I have in a list from the view.

To test this I have written a Jasmine test which looks like the following:

    it('when green car is invalidLicensePlate, should not remove green car from view', function() {
        //Arrange
        var myGreenCars = undefined;
        var expectedGreenCars = [{
            licensePlateNumber: 1,
            description: "BMW",
            quantity: 1,
            greencars: [{
                licensePlateNumber: 4,
                ownersNumber: 1,
                description: 'vallicensePlateNumber green car 1',
                quantity: 1
            }, {
                licensePlateNumber: 5,
                ownersNumber: 1,
                description: 'vallicensePlateNumber green car 2',
                quantity: 2
            }]
        }, {
            licensePlateNumber: 2,
            description: "Mazda",
            quantity: 3,
            greencars: [{
                licensePlateNumber: 6,
                ownersNumber: 2,
                description: 'vallicensePlateNumber green car 3',
                quantity: 1
            }, {
                licensePlateNumber: 7,
                ownersNumber: 2,
                description: 'vallicensePlateNumber green car 4',
                quantity: 2
            }]

        }, {
            licensePlateNumber: 3,
            description: "Toyota",
            quantity: 1
        }];

        //Act
        dut = compileDirective();
        dut.removegreen carItemFromView(myGreenCars);

        //Assert
        expect(dut.pageModel.cartData.items.length).toBe(3);
        expect(dut.pageModel.cartData.items).toEqual(expectedGreenCars);
        expect(dut.pageModel.cartData.items[0].greencars.length).toBe(2);
        expect(dut.pageModel.cartData.items[1].greencars.length).toBe(2);
    });

My test fails at dut.removegreen carItemFromView(myGreenCars);

I get the

'undefined' in not an object (evaluating 'car.greenCars')

error.

What am I doing wrong here ? this is my first time doing Jasmine tests, I am a little lost, thank you in advance.

Aucun commentaire:

Enregistrer un commentaire