一直在搜索,但找不到我这个小问题的答案。只是试着从我的控制器中测试这段代码:
$scope.viewClientAssets = (id) ->
$location.path("/assets").search("client_id", "#{id}") 然后返回这个url:
id=19
然而,当单元测试时,这一切都很好.一些假设:我的规范设置正确,就像我有其他测试和期望一样,工作非常好,所以下面是测试:
it 'checks if view client assets path is correct', ->
createController()
#gets rid of unnecessary function calls
flushRequests()
#calls the ctrl function with necessary args
$scope.viewClientAssets(clientData.client.id)
spyOn($location, 'path')
spyOn($location, 'search') #tried diff methods here such as
# and.returnValue(), .calls.any(), calls.all()
expect($location.path).toHaveBeenCalledWith("/assets")
expect($location.path.search).toHaveBeenCalled()然而,所有其他测试都通过了,但当被击中时,我会得到以下错误:
TypeError:无法读取未定义的属性“搜索”
控制台调试器告诉我:
$scope.createClientAsset = function(id) {
return $location.path('/assets/create').search("client_id", "" + id);
};那条路是未知的?
有人有什么想法吗?
发布于 2015-04-03 11:19:53
您正在监视$location.path而不返回任何东西,或者返回未定义的内容。
让您的间谍返回一个字符串(您通常期望的字符串,实际路径)和搜索函数(为javascript中的所有字符串定义)将在您的测试中正常工作。
例如,如下所示:
spyOn($location, 'path').and.returnValue('/client_id/')
$location.path('anything'); // returns '/client_id/'发布于 2015-10-02 17:48:41
要测试spyOn,进行搜索,请执行以下操作
spyOn($location, 'path').and.callThrough();
spyOn($location, 'search');它将正确返回值。
https://stackoverflow.com/questions/29430841
复制相似问题