【问题标题】:TypeError: Cannot read property 'injector' of nullTypeError:无法读取 null 的属性“注入器”
【发布时间】:2016-09-17 22:37:29
【问题描述】:

当我在 jasmine and angular 2 RC5 中运行单元测试时,我收到此错误消息 «TypeError: Cannot read property 'injector' of null»。

我的单元测试代码是:

describe('Test of category services', () =>{

beforeEach(() => {

                    TestBed.configureTestingModule({
                                providers: [CategoryService ]
                            });
                });

    it('1. Add a new category', inject([CategoryService], (categoryService )  =>{


        categoryService.addCategory(category)
            .subscribe(res => {
                    expect(CATEGORY.codeCategory).toBe(res.codeCategory);
                    expect(CATEGORY.name).toBe(res.name);
                    expect(CATEGORY.taxPercentage).toBe(res.taxPercentage);
                    expect(CATEGORY.description).toBe(res.description);
                    expect(res.idCategory).toBeGreaterThan(0);
                }

            );
    }));

});

在花了很多时间寻找解决方案后,我决定寻求任何帮助。

谢谢

【问题讨论】:

  • 我在尝试使用 adapter.downgradeNg2Provider 时遇到了同样的错误。如果我找到解决方案会更新,GL

标签: angular jasmine


【解决方案1】:

我不知道您的代码有什么问题,但我知道错误通常意味着您提供的注入没有正确地通过提供程序或构造函数传递。本质上,它不在您使用它的位置提供注射服务。抱歉,我无法提供更多帮助。

我可以解读的是,您正在提供您正在测试和使用注入的服务,我假设您已经在导入注入库,但是您的服务中有 @Injectable() 装饰器吗?

【讨论】:

    【解决方案2】:

    在为最后一个版本(2.0.0)更新 angular 后,我不再有此错误消息。 谢谢大家的帮助!

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 2017-02-04
      • 2022-01-12
      • 2021-12-04
      • 2021-12-17
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多