最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - spyOn could not find an object to spy upon for start() - Stack Overflow

programmeradmin1浏览0评论

I am using angular-cli testing framework.

inside my component , I have used 'ng2-slim-loading-bar' node module.

submit(){
    this._slimLoadingBarService.start(() => {
    });
    //method operations
}

Now when I am testing this component, I have applied spyOn this service as :

beforeEach(() => {
    let slimLoadingBarService=new SlimLoadingBarService();
    demoComponent = new DemoComponent(slimLoadingBarService);
    TestBed.configureTestingModule({
        declarations: [
            DemoComponent
        ],
        providers: [
            { provide: SlimLoadingBarService, useClass: SlimLoadingBarService}
        ],
        imports: [
            SharedModule
        ]
    });
});
it('should pass data to servie', () => {
    spyOn(slimLoadingBarService,'start').and.callThrough();
   //testing code,if I remove the above service from my component, test runs fine
});

but its not working.

It throws below error:

spyOn could not find an object to spy upon for start()

I am using angular-cli testing framework.

inside my component , I have used 'ng2-slim-loading-bar' node module.

submit(){
    this._slimLoadingBarService.start(() => {
    });
    //method operations
}

Now when I am testing this component, I have applied spyOn this service as :

beforeEach(() => {
    let slimLoadingBarService=new SlimLoadingBarService();
    demoComponent = new DemoComponent(slimLoadingBarService);
    TestBed.configureTestingModule({
        declarations: [
            DemoComponent
        ],
        providers: [
            { provide: SlimLoadingBarService, useClass: SlimLoadingBarService}
        ],
        imports: [
            SharedModule
        ]
    });
});
it('should pass data to servie', () => {
    spyOn(slimLoadingBarService,'start').and.callThrough();
   //testing code,if I remove the above service from my component, test runs fine
});

but its not working.

It throws below error:

spyOn could not find an object to spy upon for start()

Share Improve this question edited Nov 8, 2017 at 12:23 Chandan Rauniyar 8241 gold badge14 silver badges24 bronze badges asked Oct 18, 2016 at 11:18 Bhushan GadekarBhushan Gadekar 13.8k21 gold badges88 silver badges131 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

Declaring slimLoadingBarService with let, you are constraining its scope to the beforeEach callback scope. Declare it with var, or better, declare it after the proper describe() block and set its content within beforeEach callback function:

describe("some describe statement" , function(){
    let slimLoadingBarService = null;

    beforeEach( () => {
        slimLoadingBarService=new SlimLoadingBarService();

    });

    it('should pass data to service', () => {
        spyOn(slimLoadingBarService,'start').and.callThrough();
       //testing code,if I remove the above service from my component, test runs fine
    });
});

it's due to non declaration in beforeEach

updated syntax after angular 10

beforeEach(() => {
    slimLoadingBarService = TestBed.inject(SlimLoadingBarService);
});

before angular 10

beforeEach(() => {
    slimLoadingBarService = TestBed.get(SlimLoadingBarService);
});
发布评论

评论列表(0)

  1. 暂无评论