- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我正在学习如何在 Angular 中测试服务,并尝试在 Angular 文档中复制以下示例。
let httpClientSpy: { get: jasmine.Spy };
let heroService: HeroService;
beforeEach(() => {
// TODO: spy on other methods too
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get']);
heroService = new HeroService(<any> httpClientSpy);
});
it('should return expected heroes (HttpClient called once)', () => {
const expectedHeroes: Hero[] =
[{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));
heroService.getHeroes().subscribe(
heroes => expect(heroes).toEqual(expectedHeroes, 'expected heroes'),
fail
);
expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
});
我试图按字面意思复制它,但它给了我以下错误:
ERROR in src/app/services/find-locals.service.spec.ts(17,38): error TS2304: Cannot find name 'asyncData'.
有人可以帮我替换这个吗?或者告诉我我可能在其他地方做错了什么?
这是从 Angular 文档复制的测试文件:
import {FindLocalsService} from './find-locals.service';
import {HttpClient, HttpClientModule} from '@angular/common/http';
let findLocalsService: FindLocalsService;
let httpClientSpy: { get: jasmine.Spy, post: jasmine.Spy };
beforeEach(() => {
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get', 'post']);
findLocalsService = new FindLocalsService(<any> httpClientSpy, null);
});
it('should save location to server', function () {
const expectedData: any =
[{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
httpClientSpy.post.and.returnValue(asyncData(expectedData));
findLocalsService.saveLocation('something').subscribe(
data => expect(data).toEqual(expectedData),
fail
);
expect(httpClientSpy.post.calls.count()).toBe(1, 'one call');
});
这是服务本身
@Injectable()
export class FindLocalsService {
constructor(private http: HttpClient, private authService: AuthenticationService){}
saveLocation(locationObj){
return this.http.post(url + '/findLocals/saveLocation', locationObj);
}
getThreeClosestPlayers() {
const userId = this.authService.currentUser().user._id;
console.log('entered 3 closest service', userId);
return this.http.get(url + '/findLocals/getThreeClosestPlayers/' + userId)
.pipe(
map((data: any) => data.obj),
catchError(this.handleError)
)
}
}
最佳答案
改变这一行:
httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));
使用 Observable 运算符 of()
httpClientSpy.get.and.returnValue(of(expectedHeroes));
这将返回一个可以订阅的可观察对象,并将返回 expectedHeroes。如果您使用的是 Angular 6,则可以直接从 rxjs 导入:
import {of} from 'rxjs';
关于 Angular 服务测试 : Cannot find name 'asyncData' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52283055/
我试图在 asyncData 方法中获取路由参数,如果我手动转到路由,它会起作用,但是如果我使用路由器 Push 方法,它不会得到任何参数。 这是我的 asynData 方法: async asy
我正在使用 NuxtJS 的 AsyncData 方法从 API 获取视频文件,然后作为文件对象返回到页面。 像这样: async AsyncData({$axios}){ var user_
我为授权用户和用户 token 设置了一些 cookie。 在那之后,在每个 nuxtServerInit 上,我都会检查 cookie 中的相同数据并将它们设置在存储中。 当我在某些页面中使用 as
我通过 asyncData 获取一些数据,在显示这些项目后,网站必须滚动到一个元素。 methods: { scrollTo() { // ScrollTo Element..
我正在使用 nuxtjs,我试图弄清楚是否可以从异步数据函数访问组件方法。 例如,我想做这样的事情: methods: { parseResult(data) { // do
我正在开发一个 nuxt js/vue js 项目。在其中我必须调用多个 REST API。在页面组件中,我调用 asyncData() nuxt js API 来调用 REST api。 impor
我正在运行 Nuxt,我有以下功能。我想检查是否已通过身份验证,如果没有则重定向到登录页面。我收到错误 window is undefined 这是因为据我了解 asyncdata() 是在服务器端求
所以我正在学习如何在 Angular 中测试服务,并尝试在 Angular 文档中复制以下示例。 let httpClientSpy: { get: jasmine.Spy }; let heroSe
我的问题是 Nuxt 上 asyncData 方法的 firebase 请求。当应用程序位于客户端时,请求有效,但第一次加载不起作用。那么,在服务器上,我安装了什么吗? 这是我的代码: impor
我目前使用 asyncData 用于获取 api 数据,但它只能在页面中使用(不能在组件中使用)。 但是方法可以在页面和组件中使用。 这两种方法的工作方式相同,所以我想使用方法来获取 api 数据。所
获取数据和异步数据之间的确切区别是什么。官方文档说明如下: asyncData You may want to fetch data and render it on the server-side.
我目前正在从 firebase 加载一些数据,我不想在服务器端呈现,因此可以在页面上的 asyncData 中为 SEO 编制索引。 asyncData() { return firebase.f
我是 vue.js 的新手,我有一个简单的问题。 我有如下代码: asyncData({ params, error }) { return Job.api() .fetchB
我正在尝试将我的 VueJS 应用程序转换为 NuxtJS 以使用 SSR。我一直在尝试使用 asyncData 加载数据。当我将查询添加到 'mounted() {}' 中时功能正常,但我无法让它与
我是nuxt.js的新手,想请问有没有办法在asyncData中传递数据。这是代码。 import axios from 'axios' export default { d
Nuxt 使用 asyncData 在服务器端运行代码,然后将其与数据对象合并。 我想调用电话,要求我知道用户的 IP。 I see that I can get to the req object确
我正在尝试使用 Nuxt 构建服务器端可排序表,并且我希望能够在我的 Vue data 中指定默认排序列和方向,并在我的 asyncData 中访问它功能。像这样的东西: export defaul
nuxt: 2.4.5 @nuxtjs/axios: 5.4.1 我在 asyncData 中获取数据,它持续了 5 秒,这使得加载页面非常长,它与 axios 请求有关,当我这样做时,但是在方法 c
nuxt: 2.4.5 @nuxtjs/axios: 5.4.1 我在 asyncData 中获取数据,它持续了 5 秒,这使得加载页面非常长,它与 axios 请求有关,当我这样做时,但是在方法 c
我有一个 VUEJS SSR 设置,但不知道如何解决这个问题。 我的路线是这样的: { path: '/:blogSlug', component: BlogPost, name: 'blog-p
我是一名优秀的程序员,十分优秀!