我正在尝试使用Karma和Jasmine对我的AngularJS应用程序进行单元测试.我想模仿$ http服务.为此,我使用$ httpBackend方法.以下是我要测试的服务:
angular.module('MyModule').factory('MyService', function($http, $log, $parse, $q, $timeout, $filter, MyOtherService1, MyOtherService2){
var service = {};
service.getSomething = function(id){
return $http.get('/somePath/subpath/' + id);
}
});
Run Code Online (Sandbox Code Playgroud)
我对此服务的单元测试是:
describe("myTest", function(){
var myService, $httpBackend, scope, mockMyOtherService1, mockMyOtherService2;
var myResponse =
{
foo:'bar'
};
beforeEach(module("MyModule"));
beforeEach(inject(function(_MyService_, $injector){
$httpBackend = $injector.get('$httpBackend');
myService = _MyService_;
scope = $injector.get('$rootScope').$new();
mockMyOtherService1 = $injector.get('MyOtherService1');
mockMyOtherService2 = $injector.get('MyOtherService2');
}));
beforeEach(function(){
//To bypass dependent requests
$httpBackend.whenGET(/\.html$/).respond(200,'');
});
//If I uncomment the below afterEach block, the same error is shown …Run Code Online (Sandbox Code Playgroud) unit-testing angularjs httpbackend karma-jasmine angularjs-ngmock
我是使用茉莉花进行角度测试的初学者,我需要测试数据类型(例如String,int等)
这是我的控制器,我的数据被实例化为null,之后它将是一个字符串:
_this.instance = {
template_fields: {
guid: null
}
}
// It becomes string
<input type="radio" ng-model="prCreateCtrl.instance.template_fields.guid" value="{{template.guid}}" required>
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
beforeEach(inject(function ($state, $controller, $rootScope, $q) {
state = $state.get('app.provision');
ctrl = $controller('ProvisionCreateCtrl', {
instance: {
template_fields: {
guid : {}
}
}
});
}));
it('should resolve data', function () {
expect(ctrl.instance.template_fields.guid instanceof String).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)
我不明白我什么时候应该给一个字符串,我该如何测试它.正确使用instanceof String吗?