GSt*_*Sto 1 unit-testing jasmine angularjs
我有一个包含项目资源的模块,代码如下所示:
editor_services.js
var editorServices = angular.module('editorServices', ['ngResource']);
editorServices.factory('Project', ['$resource', '$http',function($resource, $http){
//...etc
Run Code Online (Sandbox Code Playgroud)
现在我想为一个期望项目资源作为参数的控制器编写测试.如何从editorServices变量中获取由此工厂创建的项目资源的实例?
这是一个工作示例如何以角度http://plnkr.co/edit/kK5fDFIVpyZTInH1c6Vh?p=preview测试资源(或http)
基本设置是:
加载angular-mocks.js你的测试.这取代了$httpBackend模拟版本.请参阅:http://docs.angularjs.org/api/ngMock.$ httpBackend
在您的测试调用$httpBackend.expect()中创建期望被嘲笑.
当您想模拟服务器响应调用时 $httpBackend.flush()
有一点需要注意,.toEqual()茉莉花不能正常使用,$resource所以你必须像这样创建自定义匹配器:
beforeEach(function() {
this.addMatchers({
// we need to use toEqualData because the Resource hase extra properties
// which make simple .toEqual not work.
toEqualData: function(expect) {
return angular.equals(expect, this.actual);
}
});
});
| 归档时间: |
|
| 查看次数: |
1950 次 |
| 最近记录: |