jasmine-karma 中的模块“DynamicTestModule”导入的意外值“未定义”

Posted

技术标签:

【中文标题】jasmine-karma 中的模块“DynamicTestModule”导入的意外值“未定义”【英文标题】:Unexpected value 'undefined' imported by the module 'DynamicTestModule' in jasmine-karma 【发布时间】:2020-01-06 05:29:42 【问题描述】:

我正在使用 jasmine-karma 进行单元测试。 配置 -

"@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",

我在所有测试用例中都遇到相同的错误。

错误:模块“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()
    

【问题讨论】:

【参考方案1】:

当您没有正确设置TestBed 时,通常会发生这种错误。

查看您的代码,BrowserModule 后面多了一个逗号。删除它,看看它是否有效。如果这不是问题,请仔细查看您是如何设置 TestBed 的,看看是否有任何不一致之处。

【讨论】:

@安德鲁...谢谢!但我无法识别不一致之处。你能帮忙吗 我不能告诉你它是什么,因为你没有包含所有的代码。只需确保删除多余的逗号即可。然后检查以确保您在 TestBed 中的每个引用都属于有效的角度实体。

以上是关于jasmine-karma 中的模块“DynamicTestModule”导入的意外值“未定义”的主要内容,如果未能解决你的问题,请参考以下文章

SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynami

Mysql:Changes in MySQL 5.7.5 (2014-09-25, Milestone 15):innodb_buffer_pool_size parameter is dynami(

Jasmin + 业力:“错误:模块 'DynamicTestModule' 导入的意外值 'HttpClient'。请添加 @NgModule 注释。”

未捕获的错误:对 JavaScript 使用动态导入时找不到模块

PLSQL 中的动态查询错误

Mach-O 的动态链接(Lazy Bind 机制)