Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有人能解释一下如何用Jasmine测试这个AngularJS吗?_Javascript_Angularjs_Jasmine - Fatal编程技术网

Javascript 有人能解释一下如何用Jasmine测试这个AngularJS吗?

Javascript 有人能解释一下如何用Jasmine测试这个AngularJS吗?,javascript,angularjs,jasmine,Javascript,Angularjs,Jasmine,所以我有一个简单的函数: wpApp = angular.module('wpApp', ['ngRoute']); wpApp.controller("ctrlr", function($scope) { $scope.message = 'This is the page'; }); 我试着用茉莉花测试它,规格如下: describe("A suite", function() { var scope; beforeEach(inject(function($ro

所以我有一个简单的函数:

wpApp = angular.module('wpApp', ['ngRoute']);

wpApp.controller("ctrlr", function($scope) {
    $scope.message = 'This is the page';
});
我试着用茉莉花测试它,规格如下:

describe("A suite", function() {
    var scope;
    beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        $controller("controller", {
            $scope: scope
        });
    }));

    it("should have the default message", function() {
        return expect(scope.message).toBe('This is the page');
    });
});
但是,它不起作用,因为实际值是未定义的

我对AngularJS以及注射的概念都比较陌生。我一直在看StackOverflow、文档和教程,我似乎不太清楚我到底做错了什么


希望它是一个小东西。有人能帮我看看我需要更改哪些规格吗?

您需要加载模块:

beforeEach(module('wpApp'));
然后,您需要加载正确的控制器:

$controller("ctrlr", {
    $scope: scope
});
完整代码:

describe("A suite", function() {
  var scope;

  beforeEach(module('wpApp'));

  beforeEach(inject(function($rootScope, $controller) {
    scope = $rootScope.$new();
    $controller("ctrlr", {
      $scope: scope
    });
  }));

  it("should have the default message", function() {
    return expect(scope.message).toBe('This is the page');
  });
});
或:


您需要加载模块:

beforeEach(module('wpApp'));
然后,您需要加载正确的控制器:

$controller("ctrlr", {
    $scope: scope
});
完整代码:

describe("A suite", function() {
  var scope;

  beforeEach(module('wpApp'));

  beforeEach(inject(function($rootScope, $controller) {
    scope = $rootScope.$new();
    $controller("ctrlr", {
      $scope: scope
    });
  }));

  it("should have the default message", function() {
    return expect(scope.message).toBe('This is the page');
  });
});
或:


非常感谢。你能不能简单地解释一下注入函数到底在做什么?我知道它的想法是“注入”函数的参数,但为什么要给它两个参数$rootScope和$controller?你总是只给这两个吗?不客气:)这是一个解释,但如果你还有问题,请告诉我:谢谢!!你能不能简单地解释一下注入函数到底在做什么?我知道它的想法是“注入”函数的参数,但为什么要给它两个参数$rootScope和$controller?你总是只回答这两个问题吗?不客气:)这里有一个解释,但如果你还有问题,请告诉我: