由茉莉花业中的模块"DynamicTestModule"导入的意外值“未定义" [英] Unexpected value 'undefined' imported by the module 'DynamicTestModule' in jasmine-karma

查看:247
本文介绍了由茉莉花业中的模块"DynamicTestModule"导入的意外值“未定义"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用茉莉花业力进行单元测试. 配置-

I am doing unit testing using jasmine-karma. configuration -

"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/jquery": "^3.3.22",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",

对于所有测试用例,我都会遇到相同的错误.

I am getting the same error for all the test cases.

错误:模块'DynamicTestModule'导入了意外的值'undefined'

Error: Unexpected value 'undefined' imported by the module 'DynamicTestModule'

app.component.spec.ts

    describe('SignupComponent', () => {
            let component: SignupComponent;
        let fixture: ComponentFixture<SignupComponent>;
        let de: DebugElement;
        let username: DebugElement;
        let firstName: DebugElement;
        let lastName: DebugElement;
        let mobileNumber: DebugElement;
        let email: DebugElement;
        let password: DebugElement;
        let confirmPassword: DebugElement;
        let router: Router;
        beforeEach(async () => {
            TestBed.configureTestingModule({
                declarations: [SignupComponent],
                imports: [HttpClientModule, RouterTestingModule,   RouterTestingModule.withRoutes([
                ]),FormsModule, ReactiveFormsModule, BrowserModule, ,ToastrModule],
                providers: [

                { provide: SignupService, useClass: SignupService },
                { provide: ToastrService, useClass: ToastrService },
            ]
            }).compileComponents().then(() => {
                fixture = TestBed.createComponent(SignupComponent);
                component = fixture.componentInstance;
                de = fixture.debugElement.query(By.css('form'));
                router = TestBed.get(Router)
                username = fixture.debugElement.query(By.css('input[id=username]'));
                firstName = fixture.debugElement.query(By.css('input[id=firstName]'));
                lastName = fixture.debugElement.query(By.css('input[id=lastName]'));
                mobileNumber = fixture.debugElement.query(By.css('input[id=mobileNumber]'));
                email = fixture.debugElement.query(By.css('input[id=email]'));
                password = fixture.debugElement.query(By.css('input[id=password]'));
                confirmPassword = fixture.debugElement.query(By.css('input[id=confirmPassword]'));
            });
        });
        beforeEach(() => {
            fixture = TestBed.createComponent(SignupComponent);
            component = fixture.componentInstance;
            fixture.detectChanges();
        });
        it('Defining SignupComponent component', () => {
            fixture = TestBed.createComponent(SignupComponent);
            component = fixture.componentInstance;
            fixture.detectChanges();
            expect(component).toBeDefined();
        })
        it('should create component', () => {
            fixture = TestBed.createComponent(SignupComponent);
            component = fixture.componentInstance;
            fixture.detectChanges();
            expect(component).toBeTruthy();
        });
        it('SignUp form is inValid when empty', () => {
            expect(component.addRegisterData.invalid).toBeTruthy();
        });
        }));

service.stub.ts

    export class SignupStubService {
        constructor(private http: HttpClient) { }
        register(value):Observable<any>  {
            console.log("in service.ts")
            return this.http.post(baseUrl + "user/register", value)
            .pipe(map(Response => Response))

        }
    }

app.component.ts

    export class SignupComponent {

      constructor(private fb: FormBuilder, private http: HttpClient, private router: Router,
         public toastr: ToastrService, public signupservice: SignupService, ) {}
    ngOninit()
    }

推荐答案

这种错误通常在您未正确设置TestBed时发生.

This sort of error typically happens when you are not setting up your TestBed correctly.

再看一下代码,在BrowserModule之后还有一个逗号.删除它,看看是否可行.如果这不是问题,请仔细查看您如何设置TestBed并查看是否存在任何不一致之处.

And looking at your code, you have an extra comma after BrowserModule. Remove that and see if it works. If this isn't the problem, then take a good look at how you've set up TestBed and see if there are any inconsistencies.

这篇关于由茉莉花业中的模块"DynamicTestModule"导入的意外值“未定义"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆