mercredi 3 février 2016

AngularJs Broadcast Unit Testing Error

I am very new to AngularJs and doing unit testing. I am having this issue with a test I wrote and it is giving me this error, I realize there might be some errors with brackets but I am just giving a sample of the code to understand how I need to test against it.

Expected spy $broadcast to have been called with [ 'dataSamplesReceived', Object({ newDataSamples: Object({  }) }) ]

Controller

app.controller('fileUploadCtrl',
        function($scope, $rootScope, $log, fileUpload) {
     $scope.uploadFile = function () {
                    var file = $scope.sampleFile;
                    $rootScope.$broadcast("sampleFilesSelected", {
                        newSampleFiles: file
                    });
                        .success(function (data) {
                            $rootScope.$broadcast("dataSamplesReceived", {
                                newDataSamples: data
                            });
                        })
                        .error(function (data) {
                            alert("Upload failed");
                        });
                } // uploadFile

spec.js

describe('test broadcast for new file opload ', function () {
    var $controller, $rootScope, file;

    beforeEach(function () {
        module('app');
        inject(function (_$rootScope_, _$controller_) {
            $rootScope = _$rootScope_;
            spyOn($rootScope, '$broadcast');
            $controller = _$controller_;
        });
    });

    it("should select new sample files", function () {
        $controller('fileUploadCtrl', {

            $scope: $rootScope.$new(),

            file: {}
        })
        expect($rootScope.$broadcast).toHaveBeenCalledWith('sampleFilesSelected', {newSampleFiles: {}})
    });


    it("should recieve data samples", function () {
        $controller('fileUploadCtrl', {

            $scope: $rootScope.$new(),

            data: {}
        })
        expect($rootScope.$broadcast).toHaveBeenCalledWith('dataSamplesReceived', {newDataSamples: {}})
    });
})

Aucun commentaire:

Enregistrer un commentaire