0

I am fairly new to unit testing. I am building an Angular component and my test suite is Jasmine/Karma.

My first test is complaining about two issues and I'm only trying to test an initialised variable value:

TypeError: Cannot set property 'http' of undefined

TypeError: Cannot read property 'isView' of undefined

The code is pretty simple so wondering why I'm getting these errors?

myComponent.ts

sView = false;

myComponent.spec.ts

describe('myComponent', () => {
    let component: myComponent;
    let fixture: ComponentFixture<myComponent>;

    beforeEach(() => {
        TestBed.configureTestingModule({
            schemas: [NO_ERRORS_SCHEMA],
            declarations: [myComponent],
            imports: [HttpClientTestingModule],
            providers: [
                {
                    provide: myService,
                    useFactory: myServiceMock
                }
            ]
        });
        fixture = TestBed.createComponent(myComponent);
        component = fixture.componentInstance;
    });


    it('isView defaults to: false', () => {
        expect(component.isView).toEqual(false);
    });

});

What is going on here?

6
  • 1
    can you put the myComponent code as well. Commented Apr 1, 2020 at 16:24
  • @AnoopRajasekharaWarrier sView = false; is pretty much the only thing going on. Commented Apr 1, 2020 at 17:06
  • Just to confirm if every thing okay, can you try it('should create', () => { expect(component).toBeTruthy(); }); brfore the it() statement you have. Commented Apr 1, 2020 at 17:08
  • Is it passing? it('should create', () => { expect(component).toBeTruthy(); }); Commented Apr 1, 2020 at 17:13
  • Same http issue and "Expected undefined to be truthy." Commented Apr 1, 2020 at 17:21

1 Answer 1

1

Try below code:

describe('myComponent', () => {
component: myComponent;
let fixture: ComponentFixture<myComponent>;
let myServiceMock: MyServiceMock;
beforeEach(async(() => {
  myServiceMock = new MyServiceMock();
  TestBed.configureTestingModule({
  declarations: [myComponent],
  providers: [
    { provide: myService, useValue: myServiceMock }
  ]
})
  .compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(myComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

beforeEach(() => {
 component.ngOnInit();
})

it('should create the app component', () => {
 expect(component).toBeTruthy();
});
});
Sign up to request clarification or add additional context in comments.

3 Comments

myComponent should be your Component Class Name, MyServieMock is the mock service, and myService is your Service class name
There might be spelling mistake.Please give actual class name you have
If I take away the providers the http errors disappears, what can this mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.