转换角度种子茉莉单元测试的CoffeeScript [英] Converting angular-seed jasmine unit tests to coffeescript

查看:116
本文介绍了转换角度种子茉莉单元测试的CoffeeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为练习,我想转换包括从爵士咖啡脚本角种子回购善缘单元测试。特别是,我有与测试/单位/ directivesSpec.js 测试集,它定义了一个简单的增值服务的问题。这里是我的咖啡脚本code:

As an exercise, I am trying to convert the karma unit tests included in the angular-seed repo from js to coffee script. In particular, I am having problems with the tests/unit/directivesSpec.js test set, which defines a simple value service. Here is my coffee script code:

 1  describe 'directives', ->
 2  beforeEach module 'myApp.directives'
 3  
 4  describe 'app-version', ->
 5    it 'should print current version', ->
 6      module ($provide) ->
 7        $provide.value 'version', 'TEST_VER'
 8      inject ($compile, $rootScope) ->
 9        element = $compile('<span app-version></span>')($rootScope)
10        expect(element.text()).toEqual 'TEST_VER'

有似乎当咖啡脚本code围绕线编译成为一个问题 6 因为这样就变成了:

There seems to be a problem when the coffee script code is compiled around line 6 as this becomes:

module(function($provide) {
  return $provide.value('version', 'TEST_VER');
});

和导致我的测试中失败,出现错误:

And causes my tests to fail with the error:

Error: [ng:areq] Argument 'fn' is not a function, got Object
http://errors.angularjs.org/1.2.4/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20Object
    at /Data/src/ngfrontend-seed/app/vendor/angular/angular.js:78:12
    at assertArg (/Data/src/ngfrontend-seed/app/vendor/angular/angular.js:1358:11)
    at assertArgFn (/Data/src/ngfrontend-seed/app/vendor/angular/angular.js:1368:3)

如果我删除收益语句,测试运行良好。谈到在文档的例如,它不是立即清除模块功能是接受作为返回值,但收益语句似乎被打破的东西。

If I remove the return statement, the test runs fine. Referring to the example in the documentation, it's not immediately clear what the module function accepts as a return value, but the return statement seems to be breaking things.

如何解决这个问题,或者这将是明智的编写测试规格时,要坚持纯JavaScript的任何想法?

Any ideas of how to fix this, or would it be sensible to stick to plain javascript when writing test specs?

推荐答案

CoffeeScript的隐式返回从每个函数的最后EX pression。结果
但是你可以添加一个明确的空return语句是这样的:

Coffeescript implicitly returns the last expression from each function.
But you can add an explicit empty return statement like this:

6      module ($provide) ->
7        $provide.value 'version', 'TEST_VER'
8        return

这将编译:

module(function($provide) {
  $provide.value('version', 'TEST_VER'); //without return
});

又见此<一个href=\"http://stackoverflow.com/questions/7391493/is-there-any-way-to-not-return-something-using-coffeescript\">thread对于一些详细信息。

这篇关于转换角度种子茉莉单元测试的CoffeeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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