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?你总是只回答这两个问题吗?不客气:)这里有一个解释,但如果你还有问题,请告诉我: