Unexpected value 'undefined' imported by the module 'DynamicTestModule' in jasmine-karma

回眸只為那壹抹淺笑 提交于 2019-12-11 08:05:21

问题


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.

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()
    }

回答1:


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

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.



来源:https://stackoverflow.com/questions/57769446/unexpected-value-undefined-imported-by-the-module-dynamictestmodule-in-jasmi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!