日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

为什么按照 Angular 官网教程执行简单的测试代码,会遇到expect is not defined的错误消息

發(fā)布時間:2023/12/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 为什么按照 Angular 官网教程执行简单的测试代码,会遇到expect is not defined的错误消息 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Angular 官網(wǎng)的代碼:

https://angular.io/api/core/Injectable#providedin

我把這段代碼原封不動地拷貝到我的 app.module.ts ,然后執(zhí)行:

@Injectable() class UsefulService { }@Injectable() class NeedsService {constructor(public service: UsefulService) { } }const injector = Injector.create({providers:[{ provide: NeedsService, deps: [UsefulService] }, { provide: UsefulService, deps: [] }] }); expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);

遇到下面的錯誤消息:

Uncaught ReferenceError: expect is not defined

奇怪的是,Visual Studio Code 里并沒有任何語法錯誤,而且 expect 的定義也能識別到:

然而運(yùn)行時,expect 的值無法識別:not available

后來我在 medium 上一篇論文里找到了答案:

https://medium.com/@danielrob/understand-angular-testing-with-jasmine-karma-8d1384962011

realise the difference between the browser environment during a karma run vs that when it is being served by an http server

Karma 運(yùn)行在瀏覽器環(huán)境上和運(yùn)行在 HTTP 服務(wù)器(server side)是有所差異的。

當(dāng)運(yùn)行在瀏覽器里時,describe,it,expect 這些函數(shù)均無法訪問。

而運(yùn)行在服務(wù)器上時,能正常訪問:

During a karma run context, the window object has been populated with jasmine and angular-mock functions. Angular-mock came from our ‘karma.conf’ file list. Now that we understand these methods are being exposed on the window object, and where they come from, we are fit use them.

在 Karma 運(yùn)行上下文,jasmine 和 Angular-mock 相關(guān)的函數(shù),被生成并保存在 window 對象上。Angular-mock 來自 karma.conf.

既然弄清楚了原因,也就不再繼續(xù)糾結(jié) Angular 官網(wǎng)上的代碼了,將 expect 改成 console.log 即可。

更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:

總結(jié)

以上是生活随笔為你收集整理的为什么按照 Angular 官网教程执行简单的测试代码,会遇到expect is not defined的错误消息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。