【发布时间】:2017-03-14 00:36:09
【问题描述】:
我有一个简单的 Angular2 组件,包含以下内容
import { Component, OnInit, Input } from '@angular/core';
import {FooterLinksService} from './footer-links.service';
import { environment } from '../../environments/environment';
@Component({
selector: 'app-footer-links',
templateUrl: './footer-links.component.html',
styleUrls: ['./footer-links.component.css'],
providers: [FooterLinksService]
})
export class FooterLinksComponent implements OnInit {
constructor(private footerLinksService: FooterLinksService) {
let footerLinks = this.footerLinksService.LoadFooterLinks();
}
}
我正在尝试为此使用 Jasmine 编写单元测试。现在我想模拟 FooterLinksService,但我看到的大多数示例都涉及手动编写 FooterLinksServiceMock。有没有其他方法可以自动生成模拟服务,如 NSubStitute 并提供来自 footerLinksService.LoadFooterLinks 的预期返回值
【问题讨论】:
-
阅读 jasmine 文档。涵盖了模拟和间谍。
-
我实际上看到了这一点,但问题是我遇到的所有示例,即使在 Angular 站点angular.io/docs/ts/latest/guide/testing.html 上似乎都指向实际创建一个 mockedService (FooterLinksService) 而不是使用 speis?这是标准方法吗?如果是,为什么不通过间谍?
-
可能是因为文档尽量不同时引入太多的概念,同时也展示了不特定于 jasmine 的代码。如果您了解什么是 mock 和 spy,您当然可以使用 jasmine 来创建它们,而不是手动创建它们。为什么你不能?是什么阻止你尝试呢?
-
另请注意,用于测试的官方 Angular 文档确实使用了 jasmine 间谍。只需在angular.io/docs/ts/latest/guide/testing.html中搜索间谍
标签: unit-testing angular jasmine