我想对我的客户端模型/资源进行单元测试。
如果restangular
对restangular
.
我的资源:
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()
。