Javascript Angular Js:简单单元测试不起作用
情况:Javascript Angular Js:简单单元测试不起作用,javascript,angularjs,unit-testing,karma-runner,Javascript,Angularjs,Unit Testing,Karma Runner,情况: var app = angular.module( 'myApp', [] ); app.controller('InitCtrl', function( $scope, Person ) { $scope.test_person = Person.Person("Tom"); }) app.factory('Person', function(){ return { Person: function(name){ this.
var app = angular.module( 'myApp', [] );
app.controller('InitCtrl', function( $scope, Person )
{
$scope.test_person = Person.Person("Tom");
})
app.factory('Person', function(){
return {
Person: function(name){
this.name = name;
console.log ( name );
}
}
})
describe('Person', function () {
var Person;
beforeEach(module('myApp'));
beforeEach(inject(function (_Person_) {
Person = _Person_;
}));
describe('Constructor', function () {
it('assigns a name', function () {
expect(new Person('Ben')).to.have.property('name', 'Ben');
});
});
});
我正在尝试为我的angular应用程序设置单元测试。我已经创建了一个基本的测试应用程序,并编写了一个简单的单元测试,但没有按预期工作
应用程序:
var app = angular.module( 'myApp', [] );
app.controller('InitCtrl', function( $scope, Person )
{
$scope.test_person = Person.Person("Tom");
})
app.factory('Person', function(){
return {
Person: function(name){
this.name = name;
console.log ( name );
}
}
})
describe('Person', function () {
var Person;
beforeEach(module('myApp'));
beforeEach(inject(function (_Person_) {
Person = _Person_;
}));
describe('Constructor', function () {
it('assigns a name', function () {
expect(new Person('Ben')).to.have.property('name', 'Ben');
});
});
});
单元测试:
var app = angular.module( 'myApp', [] );
app.controller('InitCtrl', function( $scope, Person )
{
$scope.test_person = Person.Person("Tom");
})
app.factory('Person', function(){
return {
Person: function(name){
this.name = name;
console.log ( name );
}
}
})
describe('Person', function () {
var Person;
beforeEach(module('myApp'));
beforeEach(inject(function (_Person_) {
Person = _Person_;
}));
describe('Constructor', function () {
it('assigns a name', function () {
expect(new Person('Ben')).to.have.property('name', 'Ben');
});
});
});
错误消息:
var app = angular.module( 'myApp', [] );
app.controller('InitCtrl', function( $scope, Person )
{
$scope.test_person = Person.Person("Tom");
})
app.factory('Person', function(){
return {
Person: function(name){
this.name = name;
console.log ( name );
}
}
})
describe('Person', function () {
var Person;
beforeEach(module('myApp'));
beforeEach(inject(function (_Person_) {
Person = _Person_;
}));
describe('Constructor', function () {
it('assigns a name', function () {
expect(new Person('Ben')).to.have.property('name', 'Ben');
});
});
});
运行测试时,我会收到实际的错误消息:
PhantomJS 1.9.8 (Mac OS X 0.0.0) Person "before each" hook: workFn for "assigns a name" FAILED
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.4/$injector/nomod?p0=myApp
尝试:
var app = angular.module( 'myApp', [] );
app.controller('InitCtrl', function( $scope, Person )
{
$scope.test_person = Person.Person("Tom");
})
app.factory('Person', function(){
return {
Person: function(name){
this.name = name;
console.log ( name );
}
}
})
describe('Person', function () {
var Person;
beforeEach(module('myApp'));
beforeEach(inject(function (_Person_) {
Person = _Person_;
}));
describe('Constructor', function () {
it('assigns a name', function () {
expect(new Person('Ben')).to.have.property('name', 'Ben');
});
});
});
我尝试过使用不同的sintax,如下所示:
var app = angular.module( 'myApp' );
但这并不能解决问题
问题:
var app = angular.module( 'myApp', [] );
app.controller('InitCtrl', function( $scope, Person )
{
$scope.test_person = Person.Person("Tom");
})
app.factory('Person', function(){
return {
Person: function(name){
this.name = name;
console.log ( name );
}
}
})
describe('Person', function () {
var Person;
beforeEach(module('myApp'));
beforeEach(inject(function (_Person_) {
Person = _Person_;
}));
describe('Constructor', function () {
it('assigns a name', function () {
expect(new Person('Ben')).to.have.property('name', 'Ben');
});
});
});
这个简单的测试有什么问题吗?应用程序中缺少某些内容,或者测试中出现错误
谢谢大家! 最可能的情况是,您没有将app.js加载到您的:
非常感谢。事实上,它没有做到这一点。我仍然收到一个错误,但至少测试已经运行:Person构造函数分配了一个名称FAILED TypeError:“[object object]”不是构造函数(正在评估“new Person('Ben'))