简体   繁体   English

AngularJS:基于Google Maps API的单元测试应用程序

[英]AngularJS: unit testing application based on Google Maps API

I need to write unit tests for AngularJS application which heavily uses Google Maps API v3 (calculating distances, creating markers in the map etc.) and I know that I should somehow create or mock map canvas so I could unit test functions that use Google Maps API and create markers on that canvas but I am not sure how to do that and I was not able to find any good tutorial/resource on how to unit test (preferably with AngularJS/Jasmine) applications based on Google Maps API. 我需要为AngularJS应用程序编写单元测试,该应用程序大量使用Google Maps API v3(计算距离,在地图中创建标记等),我知道我应该以某种方式创建或模拟地图画布,以便我可以单元测试使用Google Maps的函数API并在该画布上创建标记,但我不知道如何做到这一点,我无法找到任何关于如何基于Google Maps API进行单元测试(最好使用AngularJS / Jasmine)应用程序的好教程/资源。

Any working example - even the simplest one - of unit tests like this would be greatly appreciated. 任何工作示例 - 即使是最简单的 - 像这样的单元测试将非常感激。

From your comments above, it seems like maybe you need the following information (if you don't please disregard): This is a LOT of explanation I'd have to give you that will amount to a novel if I typed it all up. 从你上面的评论来看,似乎你可能需要以下信息(如果你不要忘记):这是一个很多解释我必须给你的,如果我把它全部输了,将相当于一本小说。 As such, I'm just going to link you a LOT of articles that will do a much better job than I can of explaining each piece to you. 因此,我只想给你链接很多文章,这些文章将比我向你解释每件作品做得更好。

Angular is all about dependency injection . Angular就是依赖注入 Dependency injection is vital if you're doing any unit testing. 如果您正在进行任何单元测试,依赖注入至关重要 For sake of completeness I'll assume you don't know what dependency injection is and provide a quick explanation (forgive me if you already know this): Dependency injection is designing your code such that any external dependencies can be "injected" via an argument. 为了完整起见,我假设您不知道依赖注入是什么并提供快速解释(请原谅我,如果您已经知道这一点):依赖注入是设计您的代码,以便任何外部依赖可以通过一个“注入”论点。 A dependency would be any code external to the block of code it's in. This is why in Angular you have to include $scope in your controllers, or maybe $http or $resource... because those are being injected into your controller. 依赖关系将是它所在的代码块外部的任何代码。这就是为什么在Angular中你必须在你的控制器中包含$ scope,或者可能是$ http或$ resource ...因为那些被注入你的控制器。 In unit testing, this allows you to mock up those objects and pass them in, so you can see test outcomes in a controlled way. 在单元测试中,这允许您模拟这些对象并将其传递,因此您可以以受控方式查看测试结果。

If you're going to use some external code (Google Maps API, Facebook API, etc) in your controller, you want to inject that code by wrapping it in a service and injecting it into your controller . 如果您要在控制器中使用一些外部代码(Google Maps API,Facebook API等),您希望通过将其包装在服务中并将其注入控制器注入该代码。

Also, you may want to create a directive for the actual map piece as DOM manipulation (such as what's done by new Map() in the Google Maps API) should be done in the directive. 此外,您可能希望为实际地图片段创建指令 ,因为DOM操作(例如,在Google Maps API中由新Map()完成的操作)应该在指令中完成。 Then you'd just test the directive. 然后你只是测试指令。 For guidance on testing directives, I'd advise looking to Angular's directive tests in their Github repository as examples . 关于测试指令的指导,我建议在他们的Github存储库中查看Angular的指令测试作为示例 Basically you $compile the directives, and test the outcomes of manipulating it. 基本上你是$编译指令,并测试操作它的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM