我在Angular 2.0.0中遇到了TestBed的问题
beforeEach( async(() => {
TestBed.configureTestingModule({
declarations: [
SomethingComponent
]
}).compileComponents(); // compile template
}));
beforeEach(() => {
fixture = TestBed.createComponent(SomethingComponent);
comp = fixture.componentInstance;
});
it('test', async(() => {
fixture.detectChanges();
expect(true)
}));
Run Code Online (Sandbox Code Playgroud)
给定组件:
@Component({
templateUrl: 'app/components/something/something.component.html'
})
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Firefox 48.0.0 (Mac OS X 10.11.0)
Failed: Uncaught (in promise): Failed to load app/components/something/something.component.html
resolvePromise@node_modules/zone.js/dist/zone.js:429:31
resolvePromise@node_modules/zone.js/dist/zone.js:414:17
scheduleResolveOrReject/<@node_modules/zone.js/dist/zone.js:462:17
ZoneDelegate.prototype.invokeTask@node_modules/zone.js/dist/zone.js:236:23
ProxyZoneSpec.prototype.onInvokeTask@node_modules/zone.js/dist/proxy.js:96:20
ZoneDelegate.prototype.invokeTask@node_modules/zone.js/dist/zone.js:235:23
Zone.prototype.runTask@node_modules/zone.js/dist/zone.js:136:28
drainMicroTaskQueue@node_modules/zone.js/dist/zone.js:368:25
ZoneTask/this.invoke@node_modules/zone.js/dist/zone.js:308:25
Run Code Online (Sandbox Code Playgroud)
我想这可能是因为配置没有正确设置.我正在使用gulp将TypeScript预编译为JavaScript,我有以下项目结构:
|_ grails-app
|_ frontend
|_ angular
|_ app
| |_ components
| | |_ something
| | …Run Code Online (Sandbox Code Playgroud) 我在Angular 5项目中使用ng2-translate,我正在尝试为一个组件创建单元测试.我总是导入TranslateModule.forRoot( *...* )我的测试,测试将在我的视图中使用翻译管道.
但是在两种情况下,在单元测试期间会抛出上述错误:我认为其他工作测试没有任何差别.
×应该创建(44ms)TypeError:无法在Object上的TranslatePipe.transform(webpack:///./node_modules/ng2-translate/src/translate.pipe.js?:74:75)读取未定义的属性'subscribe'. eval [as updateRenderer](ng:///DynamicTestModule/MyComponent.ngfactory.js:127:70)at Object.debugUpdateRenderer [as updateRenderer](webpack:///./node_modules/@angular/core/esm5/core. js?:14951:21)checkAndUpdateView(webpack:///./node_modules/@angular/core/esm5/core.js?:14065:14)
这可能发生的原因是什么?我不使用TranslateService,但我在模板中使用管道.有没有人遇到同样的问题?
我正在尝试使用开发应用程序服务器在Python中测试Google App Engine的全新全文搜索功能.
是否有一个存根search,允许用testbed本地单元测试进行测试?
以下是抛出异常的示例代码:
#!/usr/bin/python
from google.appengine.ext import testbed
from google.appengine.api import search
def foo():
d = search.Document(doc_id='X',
fields=[search.TextField(name='abc', value='123')])
s = search.Index(name='one').add(d)
tb = testbed.Testbed()
tb.activate()
# tb.init_search_stub() ## does this exist?
foo()
Run Code Online (Sandbox Code Playgroud)
抛出的异常foo()是:AssertionError: No api proxy found for service "search".是否为搜索编写了api代理?
赞赏的想法和评论.
我有一个组件,我正在尝试使用TestBed进行设置和测试.
该组件包含一个类,该构造函数中的参数是一个接口,而不是具体的类.我选择使用的任何类(无论是真实的,还是用于单元测试的mok)都满足此接口.但是,当我构建在TestBed中使用此服务的组件时,我无法弄清楚如何将该参数定义到TestBed配置.
以下是组件的TestBed配置:
describe('PanelContentAreaComponent', () => {
let component: PanelContentAreaComponent;
let fixture: ComponentFixture<PanelContentAreaComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ PanelContentAreaComponent
],
providers:[
MenuCommandService, ProcedureDataService, IOpenService],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)
在TestBed中构造有问题的服务是ProcedureDataService.它的定义如下:
@Injectable()
export class ProcedureDataService {
serverOpenFile: OpenFile;
constructor(private _openService: IOpenService) {
this.serverOpenFile = emptyFileStatus;
}
Run Code Online (Sandbox Code Playgroud)
构造函数中的一个参数ProcedureDataService是IOpenService其定义为:
export interface IOpenService {
openFile(fileType: string, dataType: string, filePath: string) ;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这是一个接口,而不是具体的类.
在我的服务单元测试中,我们通过如下实现来模拟IOpenService:
export class mockOpenService implements IOpenService{
constructor(){}
openFile(fileType: string, dataType: string, filePath: string) {
let fileContent: …Run Code Online (Sandbox Code Playgroud) 我需要在 Angular 中测试一个组件,它只有一种方法和某些 @Input 和 @Output 属性-
updateColumns(eventValue: ManagedColumns) {
this.applyColumnChanges.emit(eventValue);
}
Run Code Online (Sandbox Code Playgroud)
还有另一个组件具有应该调用上述方法的方法。调用因为它正在发出一个事件,我相信该事件将被另一个事件消耗,而后者又会发出另一个事件,该事件被另一个组件中的第三个消耗 -
applyChanges() {
this.apply.emit(<ManagedColumns>{
selectedColumns: this.selectedItems.slice(),
availableColumns: this.availableItems.slice()
});
this.closeDialog();
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试测试 updateColumns 但不知道该怎么做?是否可以模拟 applyChanges 依次发送到 updateColumns 并且我们可以检查相同的值?
如果我试过——
manageColumnsComponent = TestBed.createComponent(ManageColumnsComponent).componentInstance;
spyOn(manageColumnsComponent.applyChanges, 'emit');
Run Code Online (Sandbox Code Playgroud)
得到错误 -
[ts] Argument of type '"emit"' is not assignable to parameter of type 'never'.
Run Code Online (Sandbox Code Playgroud) 你能告诉我如何解决这个问题吗?
登录.组件.规格.ts
const mockModel = {
url: 'login',
model: {
handle: 'test@test.com',
password: '123456789'
}
}
export class HttpCommonServiceMock {
public post(url: any, model: any): any { }
}
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule, CoreModule, HttpModule],
declarations: [],
providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
{ provide: HttpCommonService, useClass: HttpCommonServiceMock },
]
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
component.ngOnInit();
});
it('should create …Run Code Online (Sandbox Code Playgroud) 嘿,我是 angular 6(又名 angular)测试的新手,我有一个问题,就是要重新评估迄今为止我看到的每一个测试。
我们先来看看简单组件的简单测试(由cli生成)
describe('CompComponent', () => {
let component: CompComponent;
let fixture: ComponentFixture<CompComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CompComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CompComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)
我有一个主要问题:
1.我怎么知道每个 Async beforeEach 调用是在每个测试单元(又名它)之前完成的?是否有任何情况下此调用会在每个它之后发生,因为它毕竟是异步调用?
我是 Angular 和 jhipster 的新手,我编辑了登录组件,添加了 formbuilder 和 MatDialogRef 并更新了单元测试:
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatDialogRef } from '@angular/material/dialog';
/* Other imports */
@Component({
selector: 'jhi-login-modal',
templateUrl: './login.component.html',
styleUrls: ['login.scss']
})
export class JhiLoginModalComponent implements OnInit {
authenticationError: boolean;
hide = true;
loginForm: FormGroup;
constructor(
private readonly eventManager: JhiEventManager,
private readonly loginService: LoginService,
private readonly stateStorageService: StateStorageService,
private readonly router: Router,
private readonly fb: FormBuilder,
private readonly dialogRef: MatDialogRef<JhiLoginModalComponent>
) {}
ngOnInit(): void {
this.loginForm = …Run Code Online (Sandbox Code Playgroud) 我ResizeObserver在我的组件中使用过,它工作正常。但是运行ut时出现这样的错误:
ReferenceError: ResizeObserver is not defined
133 | });
134 |
> 135 | this.resizeObserver = new ResizeObserver((entries) => {
| ^
136 | const entry = entries.find((e) => e.target === this.wrapper._elementRef.nativeElement);
137 | if (entry && entry.contentRect) {
138 | if (this.select && this.select.isOpen) {
Run Code Online (Sandbox Code Playgroud)
我使用 TestBed 创建组件:
fixture = TestBed.createComponent(MyComponent);
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会出现这个错误,我只是新建了一个对象。
ts 版本
"rxjs": "~6.5.5",
"tslib": "^2.0.0",
"zone.js": "~0.10.3"
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我已使用 Angular 12 应用程序将 Angular 12 应用程序更新为 Angular 14 ng update。构建成功,可以看到我的应用程序运行得很好,但我的测试失败了。我遇到的错误之一是:
TestBedStatic 类型上不存在属性“configureTestingModule”
有人知道这个问题有什么解决方法吗?我需要更新我的测试库吗?
@angular/core: ^14.2.0
jasmine-core: ~3.8.0
jasmine-marbles: ^0.8.3
karma: ~6.3.0
protractor: ^7.0.0
Run Code Online (Sandbox Code Playgroud)
样品测试:
beforeEach( () => {
TestBed.configureTestingModule({
providers: []
});
})
Run Code Online (Sandbox Code Playgroud) testbed ×10
angular ×9
angular-test ×4
jasmine ×2
unit-testing ×2
angular11 ×1
angular12 ×1
angular14 ×1
gulp ×1
javascript ×1
jhipster ×1
mocking ×1
testing ×1
typescript ×1
url-template ×1