【发布时间】:2018-11-08 13:14:01
【问题描述】:
在 Angular 6 中为动态内容编写测试用例时需要帮助,以及 使用 Karma 包。
spec.ts:
我写了测试用例来检查文章的观点。前 执行该函数,它正在传递,但在获取数据后它是 不通过。如何为动态内容编写测试用例。
describe('SingleArticleVideoComponent', () => {
let originalTimeout;
let debugTest: DebugElement[];
let el: HTMLElement;
let component: SingleArticleVideoComponent;
let fixture: ComponentFixture<SingleArticleVideoComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SingleArticleVideoComponent,
PollsComponent,
AdBannerComponent],
imports: [
RouterTestingModule,
NavModule,
FooterModule,
VgCoreModule,
VgControlsModule,
VgOverlayPlayModule,
VgBufferingModule,
FormsModule,
ReactiveFormsModule,
MatFormFieldModule,
MatRadioModule,
MatDialogModule,
HttpModule,
HttpClientModule,
BrowserAnimationsModule,
BrowserModule
],
providers: [
ArticleService,
AdService,
UserServiceService
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SingleArticleVideoComponent);
component = fixture.componentInstance;
fixture.detectChanges();
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;
});
afterEach(function() {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
fixture = TestBed.createComponent(SingleArticleVideoComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('views should be more than 100', async(() => {
expect(component.anchor).toEqual('before');
expect(component.anchor).toEqual('after');
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});
component.ts:
当我使用 ng serve 时,它给出了正确的结果,但它不是 使用 ng test 进行测试。
ngOnInit() {
this.get_single_video('emcure-csi-tv-dr-pk-dep-ACE-inhibitor-or-ARNI-what-should-be-used-in-heart-failure-with-reduced-ejection-fraction', this.category);
}
get_single_video(slug, category) {
console.log('get one video calling');
this.anchor ='before';
this.service.get_single_video(slug, category).subscribe(
data => {
if(data['success'])
{
this.anchor='after';
this.load_data = true;
if(data['data']['guest3'].length > 0 || data['data']['guest4'].length > 0){
this.gus = true;
}
}
});
component.html:
当我控制台它时,Views 给出了 null。视图正常显示 如果我使用 ng serve 运行它,而不是 ng test。
<li class="views"><code>{{single_article['anchor']}}</code><br>Views</li>
user-service.service.ts:
我可以在 map 函数中看到 data.json(),但我无法获取 正如我提到的,在 component.ts 文件中的 subscribe 函数中 以上。
import 'rxjs/Rx';
get_single_video(slug, catagory) {
console.log('in article service single video');
const final_url = this.api_url + '/' + slug + '?key=' + this.api_key;
console.log(final_url);
return this._http.get(final_url)
.map(data => {
data.json();
// the console.log(...) line prevents your code from working
// either remove it or add the line below (return ...)
console.log(' I CAN SEE DATA HERE: ', data.json());
return data.json();
}).catch(error => observableThrowError(error.json()));
}
【问题讨论】:
-
我在下面的回答有帮助吗?
标签: angular unit-testing jasmine karma-jasmine karma-runner