我正在使用Jasmine来测试我的服务.我的一项服务使用$routeParams作为URL参数.现在当我测试时,$routeParams变得不确定
这是我的服务代码
this.getProjectFunction = function (options) {
$http.get(rootUrl + $routeParams.projectName)
.success(options.success)
.error(options.error);
};
这就是我的测试的样子
describe('App Service', function() {
describe('App Service Tests', function(){
var httpBackend, service, optionsSpy, routeParams;
var returnData = [{"id":1,"name":"test"];
beforeEach( module( 'appName' ) );
beforeEach(
inject(
function($httpBackend,projectService,routeParams) {
service = projectService;
optionsSpy = jasmine.createSpyObj('optionsSpy',['success','error','data']);
routeParams = $routeParams;
httpBackend = $httpBackend;
}
)
);
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
//this should get a specific project
it('should call the getAllProjectsFunction function that will return an argument array',
function(){
//set up some data for the http call to return and test later.
httpBackend.whenGET('../'+routeParams.projectName).respond(returnData);
service.getProjectFunction(optionsSpy);
httpBackend.flush();
expect(optionsSpy.success.mostRecentCall.args[0]).toBe(returnData);
}
);
});
});
我是茉莉花测试的新手.感谢您的帮助,祝您有美好的一天:)
解决方法:
您需要注入$routeParams而不是routeParams,然后可以设置routeParams.projectName =’foo’;然后设置httpBackend.whenGET(‘../ foo’).respond(returnData); (../foo可能需要更像/ foo,因为我认为“上一个文件夹”命令在这里不起作用.)