I've followed this tutorial:
http://tech.pro/tutorial/1515/you-got-http-in-my-angularjs-unit-tests
And changed my code (below) I'm testing the service individually but some
how is not calling the service. It's not entering in the promise.then().
Do you know why ?
TESTS
describe('ContactController - Testing CRUD Operations', function(){
> var ContactServiceMock,
> contactsMock,
> httpBackend,
> baseURL;
>
> beforeEach(module('contactListApp', 'itemsMock'));
>
> // Initialize the controller and a mock scope
> beforeEach( inject(function (_ContactService_, CONTACTS_MOCK,
> _$httpBackend_, BASE_URL) {
> ContactServiceMock = _ContactService_;
> httpBackend = _$httpBackend_;
> contactsMock = CONTACTS_MOCK;
> baseURL = BASE_URL;
> }));
> it('should find all contacts', function(){
> var expected,
> contactsCalled,
> promise;
> httpBackend.expectGET(baseURL + '/contacts').respond(contactsMock);
> expect(ContactServiceMock.findAll()).toBeDefined();
>
> promise = ContactServiceMock.findAll();
> promise.then(function (contacts){
> contactsCalled = contacts;
> });
> httpBackend.flush(); //flushes any request then allow then() call
> to be executed when the promise is resolved in the success()
> expect(contactsCalled.length).toBeGreaterThan(0);
> });
> })
and my service:
this.findAll = function () {
> var deferred = $q.defer(); //deferred represents a task that will
> be done in the future
>
> $http.get(config.BASE_URL + '/contacts')
> .success(function(data, status) {
> deferred.resolve(data);
> })
> .error(function(data) {
> console.log('Error loading Contacts: ' + data);
> deferred.reject(data);
> });
> return deferred.promise;
> };
Do you know what is happening ?!
The error is ... don't enter in then() code and throws an error: no
pending request to flush !
Thanks !!!
Em sexta-feira, 16 de maio de 2014 09h07min49s UTC-3, Garry Taylor escreveu:
>
> Try the following:
>
> expect($scope.hasAlertVisible).toBe("false");
>
> Also does the ContactController have something like
>
> $scope.hasAlertVisible = "false";
>
> Regards,
>
> Garry Taylor
>
> On Thursday, 15 May 2014 04:26:55 UTC+1, Diego Freitas wrote:
>>
>> Hi there,
>>
>> I'm facing a problem that I really don't know the reason of it ...
>>
>> My controller is undefined ...
>>
>> *karma.config:*
>>
>>> files: [
>>> '../contents/js/jquery-1.10.2.js',
>>> '../contents/js/angular.min.js',
>>> '../contents/js/angular-route.js',
>>> '../contents/js/angular-mocks.js',
>>> '../contents/js/bootstrap.js',
>>> '../contents/js/bootstrap.min.js',
>>> '../contents/js/*.js',
>>> '../app/app.js',
>>> '../app/controllers/*.js',
>>> '../app/services/*.js',
>>> '../app/*',
>>> {pattern: 'spec/**/*Spec.js', included: true},
>>> ],
>>
>>
>> *Controller and service and app*
>>
>> var moduleApp = angular.module('contactListApp', ['ngRoute',
>>> 'ui.bootstrap']);
>>> moduleApp.controller('ContactController', function ($scope, $modal,
>>> $timeout, ContactService) { ....}
>>> moduleApp.service('ContactService', function ($http, $q, config) { ...}
>>
>>
>>
>> *Test:*
>> describe('ContactController - Testing CRUD Operations', function(){
>> var ContactController, scope, ContactService;
>>
>> beforeEach(module('contactListApp'));
>> // Initialize the controller and a mock scope
>> beforeEach( inject(function ($controller, $rootScope, ContactService) {
>> scope = $rootScope.$new();
>> ContactController = $controller('ContactController', {
>> $scope: scope
>> }); *//AFTER THIS CONTROLLER STILL UNDEFINED. IF I SEE THE
>> CONTROLLER IN DEBUG MODE IT WILL ONLY HAVE AN _OBJECT ... BUT NO FUNCTIONS*
>> ContactService = ContactService;
>> }));
>>
>> it('testing', function(){
>> expect(ContactController.hasAlertVisible).toBe("false");
>> });
>> });
>>
>> Does anyone know why is not injecting ?!!
>>
>> Thanks !!!
>>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.