Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单元测试angular 2服务在注入后不运行回调_Angular_Unit Testing_Karma Jasmine - Fatal编程技术网

单元测试angular 2服务在注入后不运行回调

单元测试angular 2服务在注入后不运行回调,angular,unit-testing,karma-jasmine,Angular,Unit Testing,Karma Jasmine,因此,我已经为我的组件创建了单元测试,但我想单独为我的单个服务创建一些单元测试。但是,当我尝试注入它们时(正在测试的服务方法不是异步的) Inject从不实际运行回调!测试可以识别它的语句,但不会出错 内部的console.log语句永远不会运行,设计为失败通过的测试也不会通过,所以我假设inject无法运行 EDIT:2添加了如何使用HTTP调用存根数据代替原始示例对Angular 2/4服务进行单元测试的完整示例。单元测试服务的一个很好的例子,与官方和第三方指南有些不同 编辑:重新阅读官方指

因此,我已经为我的组件创建了单元测试,但我想单独为我的单个服务创建一些单元测试。但是,当我尝试注入它们时(正在测试的服务方法不是异步的)

Inject从不实际运行回调!测试可以识别它的语句,但不会出错


内部的console.log语句永远不会运行,设计为失败通过的测试也不会通过,所以我假设inject无法运行

EDIT:2添加了如何使用HTTP调用存根数据代替原始示例对Angular 2/4服务进行单元测试的完整示例。单元测试服务的一个很好的例子,与官方和第三方指南有些不同

编辑:重新阅读官方指南,并在上述评论中的@AnteJablanAdamović之后指出

it('should tell ROUTER to navigate when hero clicked',
  inject([Router], (router: Router) => { // ...
}));

我不确定你是否可以把它包装成一个假的异步(为什么不呢?)或者异步作为一个回调,但这是我最初问题的正确答案(怎么没有人用50+的悬赏和10+的追加投票来解决这个问题?!)

然而,下面的策略是一种更干净/更快的方法来实现这一点,而不是通过在每个“it”语句之前将其包含在其中,从而将inject粘贴到每个“it”语句中

遗憾的是因果报应或天使没有抛出任何错误或警告标志

以下是我提供的原始答案,但也是一种替代方法:


我使用testBet.get在beforearch中注入服务:比大多数指南建议的要好得多

如果您在测试服务时遇到问题,请尝试本指南:包括具有依赖关系的简单或复杂服务:

您可以使用if语句设置路径,如上面针对setupConnections的guide-link中的语句,但除非您在调用中执行了一些异常操作,否则不需要进行路径匹配,这样就可以了

注意异步不是伪异步!!!!通常我在组件单元测试中使用fakeAsync fine,但在以这种方式对这些服务进行单元测试时,我遇到了一些错误,YMMV

    it('should get suggestions for search drop down and match of mock results for test', async(() => {
        console.log('running get Suggestions');
// here we set out HTTP data response stub: put return data in body
        setupConnections(backend, {
            body: {
                suggestions:
                ["6-minute walk test",
            },
            status: 200
        });
// resolve HTTP call with subscribe and do test in call back.
        searchService.getSuggestions('test').subscribe((x) => {
            console.log(x);
            expect(x).toEqual(['6-minute walk test']);
        });

    });

您只需嵌套1个额外的
clojure
,这就是它无法工作的原因

it("should build Url String", () => {
        inject([SearchService], (searchService: SearchService) => {
            spyOn(searchService, 'buildURL');
            console.log("should be logging like a boss");
            searchService.buildURL("test", "coursename", 2);
            expect(searchService.buildURL).toHaveBeenCalled();
            expect(searchService.buildURL("test", "coursename", 2)).toBe(['1']);
            expect(searchService.buildURL("test", "coursename", 2)).toBeFalsy();
        });
    });
如下图所示进行更改以使其正常工作:

it("should build Url String", inject([SearchService], (searchService: SearchService) => {
            spyOn(searchService, 'buildURL');
            console.log("should be logging like a boss");
            searchService.buildURL("test", "coursename", 2);
            expect(searchService.buildURL).toHaveBeenCalled();
            expect(searchService.buildURL("test", "coursename", 2)).toBe(['1']);
            expect(searchService.buildURL("test", "coursename", 2)).toBeFalsy();
    })
);
原因是,由于您在另一个
clojure
范围内执行
inject
,它将在另一个范围内执行,
it
的第二个参数应该是带有测试的函数,但由于您传入了一个空的
clojure
它将简单地解析为
true

下面是一个发生了什么的例子:

() => { // calling this clojure it will return null/undefined
  () => { // calling this clojure it will return '1'
    return '1';
  }
}

如果它没有运行,应该会有错误。如果您确定没有错误,请提供@estus no errors。事实上,整个测试顺利通过。我可以在inject外部控制日志,但在console.log内部从未运行。@estus,对其进行了改进,并将其剥离到了minimumPlease,提供了一种复制问题的方法-Plunker、Stackblitz等。因为代码看起来不错,除了您之外,没有人能够找出问题所在。您确定问题不在于额外的一个clojure吗<代码>它(“应该构建Url字符串”,()=>{?尝试删除它以获取此
它(“应该构建Url字符串”),注入([SearchService],(SearchService:SearchService)=>{
it("should build Url String", () => {
        inject([SearchService], (searchService: SearchService) => {
            spyOn(searchService, 'buildURL');
            console.log("should be logging like a boss");
            searchService.buildURL("test", "coursename", 2);
            expect(searchService.buildURL).toHaveBeenCalled();
            expect(searchService.buildURL("test", "coursename", 2)).toBe(['1']);
            expect(searchService.buildURL("test", "coursename", 2)).toBeFalsy();
        });
    });
it("should build Url String", inject([SearchService], (searchService: SearchService) => {
            spyOn(searchService, 'buildURL');
            console.log("should be logging like a boss");
            searchService.buildURL("test", "coursename", 2);
            expect(searchService.buildURL).toHaveBeenCalled();
            expect(searchService.buildURL("test", "coursename", 2)).toBe(['1']);
            expect(searchService.buildURL("test", "coursename", 2)).toBeFalsy();
    })
);
() => { // calling this clojure it will return null/undefined
  () => { // calling this clojure it will return '1'
    return '1';
  }
}