如何模拟服务中函数的局部变量?Jasmine/Karma 测试 [英] How do I mock local variable for a function in a service? Jasmine/Karma tests

查看:24
本文介绍了如何模拟服务中函数的局部变量?Jasmine/Karma 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个 Angular 服务有几个功能.所有这些都使用通过文件顶部的 var varName 定义的局部变量.

An Angular service has several functions. All of them make use of a local variable defined via a var varName at the top of the file.

在对函数进行单元测试时,有没有办法模拟这个变量?

Is there any way of mocking this variable when unit-testing the function?

推荐答案

总之没有.

最简单的方法是将它放在 $scope 级别.或者也许作为 angularjs 常量

The simplest way would be to put it on $scope level. Or Maybe as an angularjs constant

角度常数

或者,如果你是一个非常疯狂的家伙,你可以制作一个脚本来改变 var 的值,但真的不要那样做,这太疯狂了.

Or if you'r a really crazy dude you can make a script that change the value of the var, but seriously don't do that, it's crazy.

这篇关于如何模拟服务中函数的局部变量?Jasmine/Karma 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆