我在我的应用程序中进行测试时遇到同样的问题。我开始使用TypeScript而且还在学习。
我正面临这个错误:
Error: [$injector:unpr] Unknown provider: movieProvider <- movie
movie.spec.ts
import { MovieService } from './movie';
describe('service MovieService', () => {
let movieService: MovieService;
beforeEach(angular.mock.module('movieSearch'));
it('should be registered', inject((movie : MovieService) => {
expect(movie).not.toBeNull();
}));
});
movie.ts
/** @ngInject */
export class MovieService {
static $inject = ['$log', '$http', 'LoaderManager'];
public apiMovieDetail: string = 'http://www.omdbapi.com/';
/** @ngInject */
constructor(private $log: angular.ILogService, private $http: angular.IHttpService, private loader: LoaderManager) {
}
public getMovie(id: string): angular.IPromise<IMovie> {
this.loader.add();
return this.$http.get(this.apiMovieDetail + '?plot=short&r=json&i=' + id).then((response: any): any => {
this.loader.remove();
return response.data;
})
.catch((error: any): any => {
this.loader.remove();
this.$log.error('XHR Failed for getMovie.\n', error.data);
});
}
}
我可以使用以下代码注入控制器:
beforeEach(inject(($controller: angular.IControllerService) => {
mainController = $controller('MainController');
}));
答案 0 :(得分:2)
以下注射工作正常。直接注入MovieService。
beforeEach(inject((MovieService: MovieService, $httpBackend: angular.IHttpBackendService, $log: angular.ILogService) => {
movieService = MovieService;
httpBackend = $httpBackend;
log = $log;
}));
但是当我使用注射“it”进行测试时,如下所示
it('should be registered', inject((movie : MovieService) => {
expect(movie).not.toBeNull();
}));
他无法解决第二个MovieService
。如果我在1个示例的工作中使用movieService
变量集。如下所示:
beforeEach(inject((movieService: MovieService) => {
movieService = $httpBackend;
}));
it('should be registered', inject(() => {
expect(movieService).not.toBeNull();
}));