How to test angular app with John Papa style, Karma, and Jasmine with data service?

北慕城南 提交于 2019-12-06 11:24:18
CSS

I get it now. Just had to look at Globally mock object in angularjs for jasmine/karma testing and my brain clicked.

The declaration and beforeEach block in the beginning of the test needs to look like this:

    var controller, scope, $location, DataService;
    var tests = 0;

    beforeEach(inject(function ($rootScope, $controller, _$location_, _DataService_) {
    $location = _$location_;
    DataService = _DataService_;
    scope = $rootScope.$new();

    controller = $controller('HomeController', {
            $scope: scope
        });
    }));

I think since I've messed with our initial setup a little too much (started the SPA from a template), I needed a strange implementation to make it all work. I'm now getting successful tests throughout:

I hope this helps someone else with similar issues.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!