我正在使用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);
}
);
});
});
我是茉莉花测试新手。感谢您的帮助,祝您有个美好的一天:)
答案 0 :(得分:3)
您需要注入$routeParams
而不是routeParams
,然后您可以设置routeParams.projectName = 'foo';
然后设置httpBackend.whenGET('../foo').respond(returnData);
(../foo
可能需要更像{ {1}}因为我不认为“up one folder”命令在这里工作。)