测试失败,如何找出原因? [英] Test fails, how to find out why?
问题描述
我正在尝试使用Jasmine和Karma学习角度测试,但是我在试图创建我的第一个测试时遇到了一些问题。
I am trying to learn testing for angular using Jasmine and Karma, but I have runned into a bit of problem trying to create my first test.
首先我得到了这个非常简单的角度应用程序,带有名为testCtrl的控制器。
First i got this very simple angular app with a controller named "testCtrl".
angular.module("testApp", [])
.controller("testCtrl", function() {
this.title = "The Title";
});
然后我得到了这个非常简单的index.html文件
I then got this very simple index.html file
<body data-ng-app="testApp">
<div data-ng-controller="testCtrl as test">
{{test.title}}
</div>
</body>
好的,所以到目前为止一切正常。角度应用正在运行。
Okay so this far everything works fine. The angular app is running.
然后我们接受了测试。
describe("Testing AngularJS Test Suite", function() {
describe("Testing AngularJS Controller", function() {
it("should initialize the title in the scope", function() {
module("testApp");
var ctrl;
inject(function($controller) {
ctrl = $controller("testCtrl", {});
});
expect(ctrl.title).toBeDefined();
expect(ctrl.title).toBe("The Title");
});
});
});
然后是业力配置文件:
// Karma configuration
// Generated on Sat Jun 04 2016 19:05:45 GMT+0200 (CEST)
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'../../bower_components/angular/angular.js',
'../../bower_components/angular-mocks/angular-mocks.js',
'../app.js',
'units/*.js'
],
proxies : {
'/' : 'http://localhost:8080/'
},
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: false,
concurrency: Infinity
})
}
这就是什么当我开始业力时,我得到了。
And this it what I get when I then start karma.
04 06 2016 19:47:27.770:WARN [config]: "/" is proxied, you should probably change urlRoot to avoid conflicts
04 06 2016 19:47:28.165:WARN [karma]: No captured browser, open http://localhost:9876/
04 06 2016 19:47:28.214:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
04 06 2016 19:47:28.248:INFO [launcher]: Starting browser PhantomJS
04 06 2016 19:47:28.882:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#ulqwsz9WukaVrW-yAAAA with id 47817079
PhantomJS 2.1.1 (Linux 0.0.0) Testing AngularJS Test Suite Testing AngularJS Controller should initialize the title in the scope FAILED
some-parentfolders/bower_components/angular/angular.js:4631:53
forEach@some-parentfolders/bower_components/angular/angular.js:322:24
loadModules@some-parentfolders/bower_components/angular/angular.js:4591:12
createInjector@some-parentfolders/bower_components/angular/angular.js:4513:30
workFn@some-parentfolders/bower_components/angular-mocks/angular-mocks.js:3060:60
inject@some-parentfolders/bower_components/angular-mocks/angular-mocks.js:3040:46
some-parentfolders/js/tests/units/testingAngularUnitSpec.js:9:13
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.049 secs / 0.038 secs)
大注意:这是一个很大的角应用程序需要HTTP服务器才能正常运行。只有这第一次测试很简单。我不确定karma是否设置了自己的HTTP服务器来运行文件,或者它是否只是将其HTTP服务器用于其他内容。我在网上的某个地方读到你应该在你自己的HTTP服务器上托管文件,然后将代理在karma配置中指向该服务器的url(正如我上面所做的那样)。不确定这个,所以请纠正我,如果我错了。
Big note: This is a big angular application that requires a HTTP-server to run properly. Only this first test is simple. I was not sure if karma setup its own HTTP-server to run the files at or if it just used its HTTP-server for other stuff. And I read somewhere on the web the you should host the file on your own HTTP-server and then point the proxy in the karma config against that url to that server (as I have done above). Not sure about this so please correct me if I am wrong.
由于我没有得到任何具体的错误信息,我站在这里线索较少,我希望有人能说出来我可能有什么想法,在哪里看或如何开始麻烦这个。
Since I dont get any specific error messages I am standing here clue less, and I was hoping someone could tell me what could be worng, where to look or how to start troubleshoting this.
更新:
尝试后使用Chrome而不是PhantomJS运行它我得到以下错误:
After trying to run it with Chrome instead of PhantomJS I get these errors:
Error: [$injector:modulerr] Failed to instantiate module testApp due to:
Error: [$injector:modulerr] Failed to instantiate module some-parentfolders/js/tests due to:
Error: [$injector:nomod] Module 'some-parentfolders/js/tests' 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.5.6/$injector/nomod?p0=some-parentfolders/js/tests
但这对我来说也没有意义吗?
But this dont make sense to me either?
推荐答案
PhantomJS可能是最佳选择在某些环境中进行单元测试,但在某些情况下它会吸收错误消息,即使使用正确的 logLevel
。
PhantomJS may be optimal for unit testing in some environments, but it tends to absorb error messages on some conditions, even with proper logLevel
.
为了更好的规范调试,Chrome启动器可能会与Karma一起使用。
For better spec debugging, Chrome launcher may be used with Karma instead.
这篇关于测试失败,如何找出原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!