Вот мой код, я сделал Plunker, поскольку код длинный:
describe("create", function(){
it("Should be defined", function(){
expect(BaseService.create).toBeDefined();
});
it("it should POST to http://api.test.com/TEST/", function(){
/***
*
* NOT TO SURE WHAT TO DO HERE
*
*/
})
});
http://plnkr.co/edit/s8P2XlkfR6HfGuj8WIcb
Я новичок в модульном тестировании, поэтому у меня возникают проблемы с тем, как утверждать следующее:
На стороне примечания я также открыт для предложений по вещам, которые я должен также проверить.
Вы можете использовать mock-сервис httpBackend, который поможет протестировать ваш $ http.
describe('Myctrl', function() {
var $httpBackend, scope, createController, authRequestHandler;
// Set up the module
beforeEach(module('MyApp'));
beforeEach(inject(function($injector) {
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
authRequestHandler = $httpBackend.when('GET', 'service.json')
.respond(true);
// Get hold of a scope (i.e. the root scope)
$rootScope = $injector.get('$rootScope');
// The $controller service is used to create instances of controllers
var $controller = $injector.get('$controller');
createController = function() {
return $controller('MyController', {'$scope' : scope});
};
})
);
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should fetch authentication token', function() {
$httpBackend.expectGET('service.json');
var controller = createController();
expect(scope.names).toBe(true);
$httpBackend.flush();
});
});