我想对我的客户端模型/资源进行单元测试.
我使用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()
.
代码:
beforeEach module ($provide)-> $provide.value('Restangular', self.restangularMock)
由coffeescript转换为
beforeEach(module(function($provide) { return $provide.value('Restangular', self.restangularMock); }));
'module'中的回调函数应返回undefined.
所以,将代码更改为:
beforeEach module ($provide)-> $provide.value('Restangular', self.restangularMock) return
将角种子茉莉花单位测试转换为coffeescript