【问题标题】:Error: Error in :0:0 caused by: Function expected错误: :0:0 中的错误,原因是:预期功能
【发布时间】:2018-01-18 09:43:51
【问题描述】:

我想模拟this._configuration.setLocalStorage_SingleValue()方法

ngOnInit() {
        //  this.getErrorLog();
        this._configuration.setLocalStorage_SingleValue('previousUrl', window.location.href);// throw error
        this.pageinationcount = this._configuration.getLocalStorage_SingleValue('previousUrl');
    }

这是我的测试方法

 it('set/ Get values to local storage', fakeAsync(() => {
        console.log(_configuration);
        console.log(_mockConfiguration);


        spyOn(_configuration, 'setLocalStorage_SingleValue')
            .and.callFake(function (key, value) {
                _mockConfiguration.setLocalStorage_SingleValue(key, value);
            });
        spyOn(_configuration, 'getLocalStorage_SingleValue')
            .and.callFake(function (key) {
                return _mockConfiguration.getLocalStorage_SingleValue(key);// mock data
            }); 

        fixture.componentInstance.ngOnInit();
        fixture.detectChanges();
        expect(fixture.componentInstance.pageinationcount).toEqual("http://localhost:9876/debug.html");
    }));

我得到了预期的值,但我在控制台窗口中的这一行 this._configuration.setLocalStorage_SingleValue('previousUrl', window.location.href); 我不知道为什么会抛出这个错误。

Error: Error in :0:0 caused by: Function expected
   at DebugAppView.prototype._rethrowWithContext (http://localhost:9876/base/src/test.ts:83266:17)
   at DebugAppView.prototype.detectChanges (http://localhost:9876/base/src/test.ts:83239:13)
   at ViewRef_.prototype.detectChanges (http://localhost:9876/base/src/test.ts:61222:9)
   at ComponentFixture.prototype._tick (http://localhost:9876/base/src/test.ts:25584:13)
   at Anonymous function (http://localhost:9876/base/src/test.ts:25598:47)
   at ZoneDelegate.prototype.invoke (http://localhost:9876/base/src/test.ts:107920:13)
   at ProxyZoneSpec.prototype.onInvoke (http://localhost:9876/base/src/test.ts:70580:13)
   at ZoneDelegate.prototype.invoke (http://localhost:9876/base/src/test.ts:107920:13)
   at onInvoke (http://localhost:9876/base/src/test.ts:31910:21)
   at ZoneDelegate.prototype.invoke (http://localhost:9876/base/src/test.ts:107920:13)

【问题讨论】:

    标签: angular unit-testing typescript jasmine karma-jasmine


    【解决方案1】:

    我明白了。当我将spyon() 代码移动到beforeach(), 内部时,错误消失了。

    beforeEach(() => {
            TestBed.configureTestingModule({
                imports: [FormsModule, MessagesModule, DataTableModule],
                declarations: [ErrorLogComponent], // declare the test component
                providers: [{ provide: ErrorLogService, useClass: ErrorLogService },
                { provide: Configuration, useClass: Configuration }]
            }).overrideComponent(ErrorLogComponent, {
                set: {
                    providers: [
                        { provide: ErrorLogService, useClass: MockMyService }, { provide: Configuration, useClass: Configuration }
                    ]
                }
            });
            fixture = TestBed.createComponent(ErrorLogComponent);
            comp = fixture.componentInstance;
            errorListService = fixture.componentRef.injector.get(ErrorLogService);
            _configuration = fixture.componentRef.injector.get(Configuration);
            spyOn(_configuration, 'setLocalStorage_SingleValue')
                .and.callFake(function (key, value) {
                    _mockConfiguration.setLocalStorage_SingleValue(key, value);
                });
            spyOn(_configuration, 'getLocalStorage_SingleValue')
                .and.callFake(function (key) {
                    return _mockConfiguration.getLocalStorage_SingleValue(key);
                });
            fixture.detectChanges();
            TestBed.compileComponents();
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-20
      • 2017-08-27
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      相关资源
      最近更新 更多