카테고리 없음

[angularjs] Angularjs 및 qunit 테스트

행복을전해요 2021. 1. 19. 09:07

컨트롤러 대신 이것을 시도하십시오

$controller('RootCtrl',['$scope',  '$rootScope', '$location','$route', function ($scope,  $rootScope, $location,  $route)  {
             $scope : this.$scope,
                          $location : this.$location
                              }]);
                              
-------------------

비슷한 문제가 있었으므로 여기에 다른 대답이 없기 때문에.

나는 결국 다음을 사용했습니다.

클라이언트 측 코드 :

var myApp= angular.module('myApp', []);

myApp.controller('myCtrl', function ($scope) {
    //angular client side code
    
        $scope.canSubmit = function () {
                //some logic
                        return true;
                            }
                            }
                            

Qunit 테스트 :

var ctrl, ctrlScope, injector;

module("Testing the controller", {
    setup: function () {
            angular.module('myApp');
                    injector = angular.injector(['ng', 'myApp']);
                    
                            ctrlScope = injector.get('$rootScope').$new();
                                    ctrl = injector.get('$controller')('myCtrl', { $scope: ctrlScope });
                                    
                                            ctrlScope.model = {
                                                        //model object
                                                                };
                                                                    },
                                                                        teardown: function () {
                                                                        
                                                                            }
                                                                            });
                                                                            
                                                                            test("Given something happened then allow submit", function () {
                                                                                ok(ctrlScope.someFunction(...), "some functionality happened");
                                                                                    equal(true, ctrlScope.canSubmit());
                                                                                    });
                                                                                    

이 블로그 게시물 이 유용했습니다.

테스트중인 컨트롤러에 더 많은 것을 쉽게 주입 할 수 있습니다.



출처
https://stackoverflow.com/questions/22008778