LoginSignup
11
11

More than 5 years have passed since last update.

CoffeeScript で AngularJS のテストを書くときに気をつけること

Posted at

module の引数に関数を指定する場合は関数の末尾に null を書かないと死ぬ

HttpInterceptor のテストを書く場合

sample-http.coffee
'use strict'

angular.module('sample.http', [])
  .factory 'sampleHttpInterceptor', ($q) ->
    request: (config) ->
      config or $q.when config
spec/sample-http.coffee
'use strict'

describe 'Service: sampleHttpInterceptor', ->

  $httpProvider = null

  beforeEach module 'sample.http'
  beforeEach module (_$httpProvider_) ->
    _$httpProvider_.interceptors.push 'sampleHttpInterceptor'
    $httpProvider = _$httpProvider_
    # ここに null とか undefined とか入れないと落ちる
    null

  it 'defined', inject (sampleHttpInterceptor) ->
    expect(sampleHttpInterceptor).toBeDefined()

  it 'intercepted', ->
    expect($httpProvider.interceptors).toContain 'sampleHttpInterceptor'

module に渡す関数で null を返さなかった場合はこんな感じのエラーになる

 Error: [ng:areq] Argument 'fn' is not a function, got $HttpProvider
        http://errors.angularjs.org/1.3.0-beta.7/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20%24HttpProvider

module に渡す関数でオブジェクトとか返すようにするとダメっぽい。
CoffeeScript だと暗黙的に最後の行を return するので普通に書くと死ぬ。

参考

Mock dep to test service error - Google グループ

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11