找不到变量:_使用jasmine测试AngularJS服务时

时间:2022-08-08 21:30:52

I am testing an AngularJS service that uses the Lodash library. When I run the tests through karma-jasmine I get an error:

我正在测试使用Lodash库的AngularJS服务。当我通过karma-jasmine运行测试时,我收到一个错误:

ReferenceError: Can't find variable: _

Is there a way to configure karma/jasmine to use Lodash?

有没有办法配置karma / jasmine使用Lodash?

1 个解决方案

#1


2  

Since you won't have a HTML file in your tests to load the script for you, you'll need to include the library in your karma config file in the files property. You can read more about it here: http://karma-runner.github.io/1.0/config/files.html

由于您的测试中没有HTML文件来为您加载脚本,因此您需要将库包含在files属性中的karma配置文件中。你可以在这里阅读更多相关信息:http://karma-runner.github.io/1.0/config/files.html

It will look something like this

它看起来像这样

In karma.conf.js

...
files: [
  'path/to/lodash.js'
]
...

Hope that helps!

希望有所帮助!

#1


2  

Since you won't have a HTML file in your tests to load the script for you, you'll need to include the library in your karma config file in the files property. You can read more about it here: http://karma-runner.github.io/1.0/config/files.html

由于您的测试中没有HTML文件来为您加载脚本,因此您需要将库包含在files属性中的karma配置文件中。你可以在这里阅读更多相关信息:http://karma-runner.github.io/1.0/config/files.html

It will look something like this

它看起来像这样

In karma.conf.js

...
files: [
  'path/to/lodash.js'
]
...

Hope that helps!

希望有所帮助!