In einem Winkeldienst habe ich $ resource Aktionsmethoden wie:
class FooService {
/*@ngInject*/
constructor($resource) {
this.Foo = $resource('/foo/:fooId/',
{fooId: '@fooId'},
{
bar: {
method: 'GET',
url: '/foo/:fooId/bar/',
isArray: true
}
);
}
getAllFoo() {
return this.Foo.query();
}
}
ist prüfbar Bar?
Ich habe bereits Tests für Methoden wie getAllFoo(), aber ich bin nicht so sicher über $ Ressourcenaktionsmethoden.
Ist es in Ordnung, sie direkt in der Steuerung zu verwenden, wie in
this.FooService.Foo.bar(params).$promise.then
?
Sollten sie getestet werden? Und wenn ja, wie?
EDIT: Ich weiß, wie getAllFoo zu testen():
describe('FooService',() => {
let $rootScope, $resource, $httpBackend, $log, $interval;
let makeFooService, translateMock;
let mockAllFoo = [
{"id": 123,"stuff": "asdf asdf"},
{"id": 124,"stuff": "hjghjg"}
];
beforeEach(window.module(FooModule.name));
beforeEach(inject((_$rootScope_, $q, _$resource_, _$httpBackend_, _$log_) => {
$rootScope = _$rootScope_;
queryDeferred = $q.defer();
$resource = _$resource_;
$httpBackend = _$httpBackend_;
$log = _$log_;
translateMock = { use:() => ({}) };
makeFooService =() => new FooService(_$resource_);
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('Service',() => {
it('has a resource-property [Foo]',() => {
let service = makeFooService();
expect(service.Foo).not.toBeNull();
});
it('should load all foos when getAllFoo() is called',() => {
$httpBackend.expect('GET', /(.+)\/foo/).respond(() => [200, mockAllFoo, 'success']);
let service = makeFooService();
let allFoo = service.getAllFoo();
$httpBackend.flush();
expect(allFoo.length).toBe(2);
});
So weiß ich, wie getAllFoo zu testen(), aber nicht, wie bar zu testen(); Wenn bar ist testbar und in einem Controller direkt verwendbar, dann sehe ich nicht den Punkt der Methoden wie getAllFoo(), die einfach um $ Ressourcen Aktion Methoden Wrapper sind. Ich brauche hier Erläuterungen zu den Best Practices.
Ich hatte gerade eine TYPO in meinem Code, $ resource Aktionsmethoden sind testbar, kein Problem damit. – Olivvv