Error in testing function call with Karma in AngularJs
I am trying to implement a very simple application with AngularJs based on the tutorial that there is in it's website. I want to perform unit testing with Karma. My files:
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
files: [
'angular/angular.min.js',
'angular/angular-resource.min.js',
'angular/angular-route.min.js',
'angular/angular-mocks.js',
'app.module.js',
'phone-list/phone-list.module.js',
'phone-list/phone-list.component.js',
'phone-detail/phone-detail.module.js',
'phone-detail/phone-detail.component.js',
'core/core.module.js',
'core/phone/phone.module.js',
'core/phone/phone.service.js',
'phone-list/phone-list.component.spec.js'
],
autoWatch: true,
frameworks: ['jasmine'],
browsers: ['Chrome', 'Firefox'],
plugins: [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine'
]
});
};
phone-list.component.js
'use strict';
angular.
module('phoneList').
component('phoneList', {
templateUrl: 'phone-list/phone-list.template.html',
controller: ['Phone', function PhoneListController(Phone) {
var self = this;
self.$onInit = initializeData;
function initializeData () {
self.orderProp = 'age';
self.loadPhones();
}
function testFunction() {
return 'test';
}
self.loadPhones = function() {
self.phones = Phone.query(testFunction);
};
}
]
});
phone-list.component.spec.js
'use strict';
describe('phoneList', function() {
beforeEach(module('phoneList'));
describe('PhoneListController', function() {
var ctrl, mockPhone, phoneData;
phoneData = {
"age": 1,
"id": "motorola",
"imageUrl": "img/phones/motorola.jpg",
"name": "Motorola XOOM with Wi-Fi",
"snippet": "The Next, Next Generation,Experience"
};
beforeEach(module('phoneList', function($provide) {
mockPhone = {
query: function () {
return phoneData;
}
};
$provide.value('Phone', mockPhone);
}));
beforeEach(inject(function($componentController) {
ctrl = $componentController('phoneList');
}));
it('should initialize controller data and check a function call', function() {
jasmine.addCustomEqualityTester(angular.equals);
spyOn(ctrl, 'loadPhones').and.callThrough();
spyOn(mockPhone, 'query').and.callThrough();
ctrl.$onInit();
expect(ctrl.loadPhones).toHaveBeenCalled();
expect(ctrl.phones).toEqual(phoneData);
expect(mockPhone.query).toHaveBeenCalledWith(jasmine.any(Function));
});
});
});
phone.service.js
angular
.module('core.phone')
.factory('Phone', ['$resource',
function ($resource) {
return $resource('phones/phones.json', {}, {
query: {
method: 'GET',
params: {phoneId: phones},
isArray: true
}
});
}
]);
package.json at C:WebstormProjectstestingappnode_moduleskarma
{
"_args": [
[
{
"raw": "karma",
"scope": null,
"escapedName": "karma",
"name": "karma",
"rawSpec": "",
"spec": "latest",
"type": "tag"
},
"C:WebstormProjectstestingapp"
]
],
"_from": "karma@latest",
"_id": "karma@1.3.0",
"_inCache": true,
"_installable": true,
"_location": "/karma",
"_nodeVersion": "6.3.0",
"_npmOperationalInternal": {
"host": "packages-12-west.internal.npmjs.com",
"tmp": "tmp/karma-1.3.0.tgz_1473445878695_0.6908389490563422"
},
"_npmUser": {
...
},
"_npmVersion": "3.10.3",
"_phantomChildren": {},
"_requested": {
"raw": "karma",
"scope": null,
"escapedName": "karma",
"name": "karma",
"rawSpec": "",
"spec": "latest",
"type": "tag"
},
"_requiredBy": [
"#DEV:/",
"#USER"
],
"_resolved": "https://registry.npmjs.org/karma/-/karma-1.3.0.tgz",
"_shasum": "b2b94e8f499fadd0069d54f9aef4a4d48ec5cc1f",
"_shrinkwrap": null,
"_spec": "karma",
"_where": "C:WebstormProjectstestingapp",
"author": {
...
},
"bin": {
"karma": "./bin/karma"
},
"bugs": {
"url": "https://github.com/karma-runner/karma/issues"
},
"config": {
"ghooks": {
"pre-commit": "npm run lint",
"commit-msg": "validate-commit-msg"
}
},
"contributors": [
....
],
"dependencies": {
"bluebird": "^3.3.0",
"body-parser": "^1.12.4",
"chokidar": "^1.4.1",
"colors": "^1.1.0",
"combine-lists": "^1.0.0",
"connect": "^3.3.5",
"core-js": "^2.2.0",
"di": "^0.0.1",
"dom-serialize": "^2.2.0",
"expand-braces": "^0.1.1",
"glob": "^7.0.3",
"graceful-fs": "^4.1.2",
"http-proxy": "^1.13.0",
"isbinaryfile": "^3.0.0",
"lodash": "^3.8.0",
"log4js": "^0.6.31",
"mime": "^1.3.4",
"minimatch": "^3.0.0",
"optimist": "^0.6.1",
"qjobs": "^1.1.4",
"range-parser": "^1.2.0",
"rimraf": "^2.3.3",
"socket.io": "1.4.7",
"source-map": "^0.5.3",
"tmp": "0.0.28",
"useragent": "^2.1.9"
},
"description": "Spectacular Test Runner for JavaScript.",
"devDependencies": {
"LiveScript": "^1.3.0",
"babel": "^6.5.2",
"babel-preset-es2015": "^6.9.0",
"babel-register": "^6.9.0",
"chai": "^3.3.0",
"chai-as-promised": "^5.0.0",
"chai-subset": "^1.2.2",
"coffee-script": "^1.9.2",
"cucumber": "^1.2.0",
"eslint": "^2.3.0",
"eslint-config-standard": "^5.1.0",
"eslint-plugin-promise": "^1.0.8",
"eslint-plugin-react": "^5.1.1",
"eslint-plugin-standard": "^1.3.1",
"ghooks": "^1.1.0",
"grunt": "^1.0.0",
"grunt-auto-release": "^0.0.6",
"grunt-browserify": "^5.0.0",
"grunt-bump": "^0.8.0",
"grunt-cli": "^1.1.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-conventional-changelog": "^6.0.1",
"grunt-conventional-github-releaser": "^1.0.0",
"grunt-cucumberjs": "^0.10.2",
"grunt-eslint": "^18.0.0",
"grunt-mocha-test": "^0.12.7",
"grunt-npm": "0.0.2",
"jasmine-core": "^2.3.4",
"json3": "^3.3.2",
"karma-browserify": "^5.0.1",
"karma-browserstack-launcher": "^1.0.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "*",
"karma-coffee-preprocessor": "*",
"karma-commonjs": "*",
"karma-coverage": "*",
"karma-firefox-launcher": "*",
"karma-growl-reporter": "*",
"karma-html2js-preprocessor": "*",
"karma-jasmine": "^1.0.2",
"karma-junit-reporter": "*",
"karma-live-preprocessor": "*",
"karma-mocha": "^1.0.1",
"karma-mocha-reporter": "^2.0.0",
"karma-ng-scenario": "*",
"karma-phantomjs-launcher": "*",
"karma-qunit": "*",
"karma-requirejs": "*",
"karma-sauce-launcher": "*",
"karma-script-launcher": "^1.0.0",
"load-grunt-tasks": "^3.1.0",
"mkdirp": "^0.5.0",
"mocha": "^2.4.1",
"mocks": "^0.0.15",
"phantomjs-prebuilt": "^2.1.3",
"proxyquire": "^1.5.0",
"qunitjs": "^1.21.0",
"requirejs": "^2.1.20",
"sinon": "^1.17.5",
"sinon-chai": "^2.7.0",
"supertest": "^2.0.0",
"supertest-as-promised": "^3.0.0",
"timer-shim": "^0.3.0",
"validate-commit-msg": "^2.4.1",
"which": "^1.1.1"
},
"directories": {},
"dist": {
"shasum": "b2b94e8f499fadd0069d54f9aef4a4d48ec5cc1f",
"tarball": "https://registry.npmjs.org/karma/-/karma-1.3.0.tgz"
},
"engines": {
"node": "0.10 || 0.12 || 4 || 5 || 6"
},
"gitHead": "9cd65cd0d9b063d54953013105904e9b55822f47",
"homepage": "http://karma-runner.github.io/",
"keywords": [
"karma",
"spectacular",
"runner",
"karma",
"js",
"javascript",
"testing",
"test",
"remote",
"execution"
],
"license": "MIT",
"main": "./lib/index",
"maintainers": [
...
],
"name": "karma",
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git://github.com/karma-runner/karma.git"
},
"scripts": {
"appveyor": "npm run lint && npm run build && npm run test:appveyor",
"build": "grunt build",
"init": "rm -rf node_modules/karma && cd node_modules && ln -nsf ../ karma && cd ../",
"init:windows": "(IF EXIST node_moduleskarma (rmdir node_moduleskarma /S /q)) && npm run link",
"link": "node --eval "path=require('path'); require('fs').symlinkSync(path.resolve(__dirname), path.resolve(__dirname, 'node_modules', 'karma'), 'junction')"",
"lint": "grunt lint",
"test": "grunt test",
"test:appveyor": "grunt test-appveyor",
"test:integration": "./scripts/integration-tests.sh",
"travis": "npm run lint && npm run build && npm test && npm run test:integration",
"unlink": "node --eval "require('fs').unlinkSync(require('path').resolve(__dirname, 'node_modules', 'karma'))""
},
"version": "1.3.0"
}
When I run the tests, I get the following error:
Expected spy query to have been called with [ <jasmine.any(Function)> ] but actual calls were [ Function ].
at Object.<anonymous> (phone-list/phone-list.component.spec.js:40:31)
Is my testing strategy wrong? Why I get this error?
链接地址: http://www.djcxy.com/p/39124.html