使用Jasmine有一种方法可以测试2个数组是否包含相同的元素,但不一定是相同的顺序吗?即
array1 = [1,2,3];
array2 = [3,2,1];
expect(array1).toEqualIgnoreOrder(array2);//should be true
Run Code Online (Sandbox Code Playgroud) 在Angular 2.0.0中,我正在测试使用Router的组件.但是我得到'提供的参数与呼叫目标的任何签名都不匹配'.错误.在spec.ts中的Visual Studio代码中,新的Router()以红色突出显示
如果有人能让我知道正确的语法是什么,我真的很感激?提前致谢.我的代码如下:
spec.ts
import { TestBed, async } from '@angular/core/testing';
import { NavToolComponent } from './nav-tool.component';
import { ComponentComm } from '../../shared/component-comm.service';
import { Router } from '@angular/router';
describe('Component: NavTool', () => {
it('should create an instance', () => {
let component = new NavToolComponent( new ComponentComm(), new Router());
expect(component).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)
组件构造函数
constructor(private componentComm: ComponentComm, private router: Router) {}
Run Code Online (Sandbox Code Playgroud) 我在我的Windows控制台上运行:
npm install -g yo grunt-cli bower
npm install -g generator-angular
yo angular
Run Code Online (Sandbox Code Playgroud)
然后我用webstorm开始我的项目并右键单击项目浏览器中的karma.conf.js文件,其中我有菜单项"Run karma.conf.js"并启动karma runner.然后我得到他的例外:
...\app\node_modules\karma\node_modules\di\lib\injector.js:9
throw error('No provider for "' + name + '"!');
^
Error: No provider for "framework:jasmine"! (Resolving: framework:jasmine)
Run Code Online (Sandbox Code Playgroud)
然后在控制台中我读到我也可以使用--force所以我试了一下:grunt --force
花了一些时间,似乎没有更多的错误.哎呀为什么--force安装提供商??? 这是在CMD中测试的.
grunt服务现在工作,它启动我的浏览器与启动应用程序.
那么什么是--force做'错误:没有提供者"框架:茉莉花"!(解决:框架:茉莉花)'消失了?
当我在webstrom IDE中咕噜咕噜时,我再次得到:
警告:没有"framework:jasmine"的提供者!(解决:框架:jasmine)使用--force继续.
所以这个问题没有解决.
我有一个现有的Angular/Laravel应用程序,其中Laravel充当角度前端的API,仅提供JSON数据.加载角度应用程序的页面index.php目前由Laravel提供.从那里开始,Angular接管了.
我正在努力开始使用Karma/Jasmine.使用karma start或karma start karma.conf.js从我的项目的根目录运行我的测试时,我收到以下错误:
ReferenceError: module is not defined
全输出:
INFO [karma]: Karma v0.12.28 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: Pattern "/Users/raph/coding/webroot/digitalocean/rugapp/public/rugapp/*.js" does not match any file.
INFO [Chrome 39.0.2171 (Mac OS X 10.9.5)]: Connected on socket 3OCUMp_xhrGtlGHwiosO with id 7897120
Chrome 39.0.2171 (Mac OS X 10.9.5) hello world encountered a declaration exception FAILED
ReferenceError: module is not defined
at Suite.<anonymous> (/Users/raph/coding/webroot/digitalocean/rugapp/tests/js/test.js:3:16)
at jasmineInterface.describe (/Users/raph/coding/webroot/digitalocean/rugapp/node_modules/karma-jasmine/lib/boot.js:59:18)
at /Users/raph/coding/webroot/digitalocean/rugapp/tests/js/test.js:1:1
Chrome …Run Code Online (Sandbox Code Playgroud) AngularJS v1.2.26
Jasmine v2.2.0
如何更改或删除某个行为spyOn?当我尝试覆盖它时,我收到以下错误:Error: getUpdate has already been spied upon
var data1 = 'foo';
var data2 = 'bar';
describe("a spec with a spy", function(){
beforeEach(module('app'));
var $q;
beforeEach(inject(function(_updateService_, _$q_){
updateService = _updateService_;
//spy the results of the getUpdate()
$q = _$q_;
var deferred = $q.defer();
deferred.resolve( data1 );
spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);
}));
describe('and here the spy should be different', function() {
it('returns a different value', function() {
var deferred = $q.defer();
deferred.resolve( data2 );
spyOn(updateService, 'getUpdate'); //ERROR …Run Code Online (Sandbox Code Playgroud) 似乎承诺不会在Angular/Jasmine测试中解决,除非你强迫一个$scope.$digest().这是愚蠢的IMO,但很好,我有适用的工作(控制器).
我现在是这种情况是我有一个并不十分关心应用程序中的任何一个范围的服务,它的作用是从服务器返回的一些数据,但承诺似乎并没有被解决.
app.service('myService', function($q) {
return {
getSomething: function() {
var deferred = $q.defer();
deferred.resolve('test');
return deferred.promise;
}
}
});
Run Code Online (Sandbox Code Playgroud)
describe('Method: getSomething', function() {
// In this case the expect()s are never executed
it('should get something', function(done) {
var promise = myService.getSomething();
promise.then(function(resp) {
expect(resp).toBe('test');
expect(1).toEqual(2);
});
done();
});
// This throws an error because done() is never called.
// Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
it('should get something', function(done) …Run Code Online (Sandbox Code Playgroud) 我的终端出现此错误:
类型错误:无法读取未定义的属性(读取“id”)
我正在尝试测试对 API 的调用,但出现错误。
我的功能:
itemToForm = () => {
this.api.send(this.component, 'get',
{ lang: 'ES', filter: { id: this.item['id'] } }
).then(resEsp => {
this.item = resEsp['data'][0];
this.api.send(this.component, 'get',
{ lang: 'EN', filter: { id: this.item['id'] } }
).then(res => {
let itemEng = res['data'][0];
let fields = this.formDef.map(register => register.filter(
field => field['register_table'].indexOf('traduction') !== -1
).map(
field => field['field_name'])
).filter(register => register.length);
fields = fields.length ? fields[0] : [];
if (itemEng) {
this.item = Object.keys(itemEng).reduce((obj, key) => { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用http://newtriks.com/2013/12/31/automating-react-with-yeoman-and-grunt/生成的项目从Grunt运行Karma/Jasmine
Karma推出PhantomJS(或Chrome),并且根据singleRun,它要么超时要么只是坐在那里什么都不做.我尝试过改变captureTimeout并browserNoActivityTimeout基于阅读有类似问题的人的解决方案,但它似乎没有用.
我的相关pacakge版本等:
我在OS X上发现有同样问题的人:
我已经尝试将所有dev依赖项更新到最新版本,但问题仍然存在.
我的控制台输出如下.引用bundle的webpack行现在是VALID/INVALID令人担忧,但我找不到任何关于它们含义的信息.这是我的控制台输出:
Running "karma:unit" (karma) task
DEBUG [config]: autoWatch set to false, because of singleRun
DEBUG [plugin]: Loading karma-* from /home/ed/workspace/wwb-app/node_modules
DEBUG [plugin]: Loading plugin /home/ed/workspace/wwb-app/node_modules/karma-chrome-launcher.
DEBUG [plugin]: Loading plugin /home/ed/workspace/wwb-app/node_modules/karma-coffee-preprocessor.
DEBUG [plugin]: Loading plugin /home/ed/workspace/wwb-app/node_modules/karma-firefox-launcher.
DEBUG [plugin]: Loading plugin /home/ed/workspace/wwb-app/node_modules/karma-html2js-preprocessor.
DEBUG [plugin]: Loading plugin /home/ed/workspace/wwb-app/node_modules/karma-jasmine.
DEBUG [plugin]: Loading plugin /home/ed/workspace/wwb-app/node_modules/karma-phantomjs-launcher.
DEBUG [plugin]: Loading plugin /home/ed/workspace/wwb-app/node_modules/karma-requirejs. …Run Code Online (Sandbox Code Playgroud) 当运行Karma来测试我的Angular4应用程序时,Found the synthetic property @enterAnimation. Please include either "BrowserAnimationsModule" or "NoopAnimationsModule" in your application.虽然我已经在app.module.ts中导入了模块但是我收到了这个错误
// animation module
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
...
@NgModule({
imports: [...
BrowserAnimationsModule,
...
],
Run Code Online (Sandbox Code Playgroud)
在我的组件中:
import { Component, OnInit } from '@angular/core';
import {
trigger,
state,
style,
animate,
transition
} from '@angular/animations';
@Component({
selector: 'app-about',
animations: [
trigger(
'enterAnimation', [
transition(':enter', [
style({ transform: 'translateX(100%)', opacity: 0 }),
animate('500ms', style({ transform: 'translateX(0)', opacity: 1 }))
]),
transition(':leave', [ …Run Code Online (Sandbox Code Playgroud) 我在我的代码中使用过像.
return $provide.decorator('aservice', function($delegate) {
$delegate.addFn = jasmine.createSpy().andReturn(true);
return $delegate;
});
Run Code Online (Sandbox Code Playgroud)
那创造了什么?我可以将createSpy调用更改为createspyobj调用.
通过使用createSpy,我们可以创建一个函数/方法模拟.Createspyobj可以做多个函数模拟.我对吗?
会有什么不同.
karma-jasmine ×10
angularjs ×5
jasmine ×5
javascript ×5
karma-runner ×4
angular ×3
typescript ×2
unit-testing ×2
angular6 ×1
angular7 ×1
node.js ×1
npm ×1
testing ×1
webpack ×1