我正在寻找像http://phpfiddle.org/这样的东西,但完全是本地的.我不想承诺安装像Apache这样复杂的东西,然后安装PHP,只是为了在我离线时试用代码.有没有什么可以在本地计算机上运行PHP 5.5而不在其下面安装整个服务器?
我是Angular 2测试的新手.我试图弄清楚在测试级别使用testsbed.get()
和使用有什么区别inject
.
例如:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SomeService]
});
const testbed = getTestBed();
someService= testbed.get(SomeService);
});
});
Run Code Online (Sandbox Code Playgroud)
VS
it('test service', inject([SomeService], (someService: SomeService) => {
Run Code Online (Sandbox Code Playgroud) 我已经提到了以下链接来获得答案,但我找不到任何适用于我的方案的解决方案. 错误:(SystemJS)无法解析ActivatedRoute的所有参数:(?,?,?,?,?,?,?,?)
因此,我一直试图从供应商中删除激活路线,但测试床仍未通过.表明
错误:没有ActivatedRoute的提供商!
所以这是我的代码,我想在使用Jasmine的角度应用程序中运行我的测试床.
import { ActivatedRoute } from '@angular/router';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterModule, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
describe('SomeComponent', () => {
let component: SomeComponent;
let fixture: ComponentFixture<SomeComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ RouterModule, RouterTestingModule ],
declarations: [ SomeComponent ],
providers: [ ActivatedRoute ],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => …
Run Code Online (Sandbox Code Playgroud) 我有一些使用Angular TestBed的单元测试.即使测试非常简单,它们运行速度也非常慢(每秒平均1次测试).
即使在重新阅读Angular文档之后,我也找不到这种糟糕性能的原因.
不使用TestBed的隔离测试只需几秒钟即可运行.
单元测试
import { Component } from "@angular/core";
import { ComponentFixture, TestBed, async } from "@angular/core/testing";
import { By } from "@angular/platform-browser";
import { DebugElement } from "@angular/core";
import { DynamicFormDropdownComponent } from "./dynamicFormDropdown.component";
import { NgbModule } from "@ng-bootstrap/ng-bootstrap";
import { FormsModule } from "@angular/forms";
import { DropdownQuestion } from "../../element/question/questionDropdown";
import { TranslateService } from "@ngx-translate/core";
import { TranslatePipeMock } from "../../../../tests-container/translate-pipe-mock";
describe("Component: dynamic drop down", () => {
let component: DynamicFormDropdownComponent;
let fixture: ComponentFixture<DynamicFormDropdownComponent>; …
Run Code Online (Sandbox Code Playgroud) 我们正在使用
TestBed.overrideComponent(CoolComponent, {
set: {
template: '<div id="fake-component">i am the fake component</div>',
selector: 'our-cool-component',
inputs: [ 'model' ]
}
})
Run Code Online (Sandbox Code Playgroud)
覆盖组件.
该组件有一个ViewChild,我们在ngOnInit方法中配置它
@Component({
selector: 'our-cool-component',
templateUrl: 'cool.component.html'
})
export class CoolComponent implements OnInit, OnDestroy {
@Input() model: SomeModel
@ViewChild(CoolChildComponent) coolChildComponent;
ngOnInit() {
this.coolChildComponent.doStuff();
}
}
Run Code Online (Sandbox Code Playgroud)
在CoolComponent
又住在一个Wrapper
组件.
当我们调用fixture.detectChanges()
上的Wrapper
夹具,这个试图构建CoolComponent,但是当它调用doStuff立即死亡(),因为CoolChildComponent
是不确定的.
有没有办法找到CoolComponent
它的存根CoolChildComponent
?看起来我们不能把它搞定,Wrapper
因为它只是通过模板引用,而不是作为组件的属性引用.
integration-testing specifications testbed typescript angular
I have got the following test:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Component } from '@angular/core';
@Component({
template: '<ul><li *ngFor="let state of values | async">{{state}}</li></ul>'
})
export class TestComponent {
values: Promise<string[]>;
}
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let element: HTMLElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
element = (<HTMLElement>fixture.nativeElement);
});
it('this test fails', async() => …
Run Code Online (Sandbox Code Playgroud) 我正在使用GAE 测试平台服务,当我运行时,users.get_current_user()
我得到None
ie
>>> import sys
>>> sys.path.append("/usr/local/google_appengine") # for Mac OS X
>>> from google.appengine.api import users
>>> from google.appengine.ext import testbed
>>> testbed = testbed.Testbed()
>>> testbed.activate()
>>> testbed.init_user_stub()
>>> users.get_current_user() == None
True
Run Code Online (Sandbox Code Playgroud)
这是预期的结果.但是,我想在运行一些单元测试时登录假用户.
如果可能(我希望它会),那么如何testbed
在从命令行运行时记录用户以便get_current_user()
返回实际用户?
谢谢阅读.
我刚刚开始使用Unit-Testing,我已经能够模拟我自己的服务以及一些Angular和Ionic,但无论我做什么都ChangeDetectorRef
保持不变.
我的意思是这是什么样的巫术?
beforeEach(async(() =>
TestBed.configureTestingModule({
declarations: [MyComponent],
providers: [
Form, DomController, ToastController, AlertController,
PopoverController,
{provide: Platform, useClass: PlatformMock},
{
provide: NavParams,
useValue: new NavParams({data: new PageData().Data})
},
{provide: ChangeDetectorRef, useClass: ChangeDetectorRefMock}
],
imports: [
FormsModule,
ReactiveFormsModule,
IonicModule
],
})
.overrideComponent(MyComponent, {
set: {
providers: [
{provide: ChangeDetectorRef, useClass: ChangeDetectorRefMock},
],
viewProviders: [
{provide: ChangeDetectorRef, useClass: ChangeDetectorRefMock},
]
}
})
.compileComponents()
.then(() => {
let fixture = TestBed.createComponent(MyComponent);
let cmp = fixture.debugElement.componentInstance;
let cdRef = fixture.debugElement.injector.get(ChangeDetectorRef);
console.log(cdRef); // logs …
Run Code Online (Sandbox Code Playgroud) 在我的一个单元测试文件中,我不得不使用不同的模拟对同一服务进行多次模拟。
import { MyService } from '../services/myservice.service';
import { MockMyService1 } from '../mocks/mockmyservice1';
import { MockMyService2 } from '../mocks/mockmyservice2';
describe('MyComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
MyComponent
],
providers: [
{ provide: MyService, useClass: MockMyService1 }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MapComponent);
mapComponent = fixture.componentInstance;
fixture.detectChanges();
});
describe('MyFirstTest', () => {
it('should test with my first mock', () => {
/**
* Test with my first mock
*/
});
});
describe('MySecondTest', () => …
Run Code Online (Sandbox Code Playgroud) 目前我正在尝试测试一个子组件,它接受来自主机组件的输入,并在ngOnInit生命周期钩子中使用,如下面的代码.
@Component({
selector: 'my-child-component',
template: '<div></div>'
})
class ChildComponent implements OnInit {
@Input() myValue: MyObject;
transformedValue: SomeOtherObject;
ngOnInit():void {
// Do some data transform requiring myValue
transformedValue = ...;
}
}
@Component({
template:`<my-child-component [myValue]="someValue"></my-child-component>`
})
class HostComponent {
someValue: MyObject = new MyObject(); // how it is initialized it is not important.
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,应该如何测试ChildComponent,其中myValue需要在创建时出现,同时能够访问ChildComponent.transformedValue以进行断言.
我尝试使用像这样的Angular TestBed类创建ChildComponent
componentFixture = testBed.createComponent(LoginFormComponent)
Run Code Online (Sandbox Code Playgroud)
但是ngOnInit已经调到我打电话的地步
fixture.componentInstance.myValue = someValue;
Run Code Online (Sandbox Code Playgroud)
我也尝试创建HostComponent的一个fixture,虽然这有效,但我仍然无法访问创建的ChildComponent实例,我需要在ChildComponent.transformedValue字段上执行断言.
非常感谢帮助!
非常感谢!
testbed ×10
angular ×7
unit-testing ×3
jasmine ×2
typescript ×2
angular-test ×1
angular5 ×1
javascript ×1
local ×1
mocking ×1
performance ×1
php ×1
provider ×1
python ×1
router ×1
testing ×1