1

我想对我的客户端模型/资源进行单元测试。

如果restangularrestangular.

我的资源:

module = angular.module 'myapp.core.resources'

class Messaging
  constructor: (@restangular) ->
    @resource = @restangular.all('messaging')

  send_to: (user, message) =>
    @resource.post(to: user.id, message: message)

module.service '$messaging', ['Restangular', Messaging]

我的模拟:

# Globally available
@restangularMock =
  one: jasmine.createSpy()
  all: (resource) ->
    post: jasmine.createSpy('post'),
    get: jasmine.createSpy('get')

我的测试:

# Set the global config before end of configuration lifecycle
angular.module('myapp.core.config').config (GlobalConfig) ->
  GlobalConfig.setBaseConfig
    api:
      baseUri: '/api',
      csrfTokens: {'messaging': 'abcdef'}

describe "Resources", ->
  beforeEach module ($provide)->
    $provide.value('Restangular', self.restangularMock)

  beforeEach module("myapp.core.resources")

  describe "#Messaging", ->
    messaging = null

    beforeEach inject ($messaging) ->
      messaging = $messaging

    it "sends a message to the given user id", ->
      messaging.send_to('test', 'message!')

      expect(self.restangularMock.all('messaging').post).toHaveBeenCalledWith(to: 'test', message: 'message!')

我得到的错误:

Error: [ng:areq] Argument 'fn' is not a function, got Object

看起来失败来自加载restangular模块,它获取restangular提供程序对象(即this.$get = -> ...)并尝试运行invoke()

4

1 回答 1

7

编码:

beforeEach module ($provide)->
    $provide.value('Restangular', self.restangularMock)

由咖啡脚本转换为

beforeEach(module(function($provide) {
  return $provide.value('Restangular', self.restangularMock);
}));

并且“模块”中的回调函数应该返回undefined

因此,将代码更改为:

beforeEach module ($provide)->
    $provide.value('Restangular', self.restangularMock)
    return

将角种子茉莉单元测试转换为咖啡脚本

于 2014-01-13T17:12:32.120 回答