【发布时间】:2018-03-15 04:05:38
【问题描述】:
所以我已经为我的组件创建了单元测试,但我希望为我的个人服务单独创建一些测试。但是,当我尝试注入它们时(正在测试的服务方法不是异步的)。
describe('SearchService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
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();
});
});
});
Inject 从未真正运行回调!测试识别 it 语句,但没有错误地通过。
里面的 console.log 语句永远不会运行,并且设计为失败的测试通过了,所以我假设注入运行失败。
【问题讨论】:
-
如果没有运行,应该有错误。如果您确定没有错误,请提供stackoverflow.com/help/mcve。
-
@estus 没有错误。事实上,整个测试以优异的成绩通过。我可以在注入之外控制日志,但在 console.log 内部永远不会运行。
-
@estus,改进它并将其剥离到最低限度
-
请提供一种方法来复制问题 - Plunker、Stackblitz 等。因为代码看起来没问题,除了你之外没有人能够找出问题所在。
-
您确定问题不在于 1 个额外的 clojure 吗?
it("should build Url String", () => {?尝试删除它以获取此it("should build Url String", inject([SearchService], (searchService: SearchService) => {
标签: angular unit-testing karma-jasmine