【发布时间】:2019-06-06 09:25:39
【问题描述】:
我已经在 Angular 6 Reactive Forms 中实现了 ngx-captcha。该功能对我来说运行良好,但是当我运行单元测试用例时,测试用例失败并显示以下消息:
TypeError: _this.reCaptchaApi.render 不是函数。
我在两个组件中实现了 ngx-captcha(可见类型)。这两个组件功能对我来说都很好,但是在单元测试用例运行期间,我遇到了上述错误。
我尝试了以下方法:
验证码测试.html
<ngx-recaptcha2 #captchaElem [siteKey]="captchaSiteKey"
formControlName="captcha"> </ngx-recaptcha2>
captca-test.ts
export class CaptchaTestComponent implements OnInit {
validSiteKey: string = environment.validSiteKey;
captchaExampleForm: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.captchaExampleForm = this.fb.group({
captcha: ['', [Validators.required]]
});
}
exampleFormSubmit(){
return false;
}
}
//captcha-test.spec.ts
import { async, ComponentFixture, TestBed } from
"@angular/core/testing";
import { HttpClientModule } from "@angular/common/http";
import { CaptchaTestComponent } from "./captcha-test.component";
import { BrowserAnimationsModule } from "@angular/platform-
browser/animations";
import { RouterTestingModule } from "@angular/router/testing";
import { MatProgressSpinnerModule, MatFormFieldModule, MatInputModule }
from "@angular/material";
import { ReactiveFormsModule, FormBuilder } from "@angular/forms";
import { TestConstants } from "src/app/test/constants";
import { NgxCaptchaModule } from "ngx-captcha";
describe("CaptchaTestComponent", () => {
jasmine.getEnv().allowRespy(true);
let fixture: ComponentFixture<CaptchaTestComponent>;
beforeEach(async(function () {
TestBed.configureTestingModule({
imports: [RouterTestingModule, BrowserAnimationsModule,
ReactiveFormsModule, HttpClientModule, MatProgressSpinnerModule,
MatFormFieldModule, MatInputModule, NgxCaptchaModule],
providers: [FormBuilder
],
declarations: [CaptchaTestComponent]
}).compileComponents();
}));
beforeEach(function () {
fixture = TestBed.createComponent(CaptchaTestComponent);
this.component = fixture.componentInstance;
this.component.captchaExampleForm.controls["captcha"].setValue(TestConstan ts.validCaptcha); //manually set a string data as input data.
fixture.detectChanges();
});
it("should init component properly", function () {
this.component.ngOnInit();
expect(this.component.captchaExampleForm).toBeDefined();
});
it("should return submit as false when we submit the form", async
function () {
const result = await this.component.exapmleFormSubmit();
expect(result).toBeFalsy();
});
});
我在 Angular 6 中使用 "ngx-captcha": "^5.0.4" 版本。
【问题讨论】:
-
请包含您尝试的整个 .spec 文件。
-
我在问题部分添加了 .spec 文件。谢谢你。 @dmcgrandle
标签: jasmine angular5 karma-jasmine angular-test