Thanks Charly for your reply.
I did exactly what you mentioned i.e. re-arranged the code so that it now
looks like this (in beforeEach block):
$httpBackend.when('GET', articleEndpoint).respond(jsonObject);
>
> ArticleDetailCtrl = $controller('ArticleDetailCtrl', { $scope: scope
> });
But some how am getting the same error. It shows id passed to api as
undefined in the error:
Error: Unexpected request: GET
> http://localhost:3000/api/articles/undefined
Am I still missing something?
Thanks
Gill
On Monday, July 7, 2014 6:54:14 PM UTC+5:30, Charly Poly wrote:
>
> Hi Gillian,
>
> The reason is that your controller make the request at startup, when
> initialized.
>
> So when you do
> ArticleDetailCtrl = $controller('ArticleDetailCtrl', { $scope: scope });
>
> in your beforeEach() block, the request start immediately.
>
> So when you set your
> $httpBackend.expectGET(articleEndpoint);
> the request is already gone.
>
> Here what you need to do, put your
> $httpBackend.expectGET(articleEndpoint);
>
> before doing
> ArticleDetailCtrl = $controller('ArticleDetailCtrl', { $scope: scope });
>
> You understand why ?
>
> Regards,
> Charly.
>
> On Monday, 7 July 2014 13:23:37 UTC+2, Gillian wrote:
>>
>> I'm trying to write a unit test for a controller which fetches article
>> details using $http service.
>>
>>
>> Controller:
>>
>> .controller('ArticleDetailCtrl',function($scope, Article,
>>> $routeParams, API_URL, ARTICLE_URL, $http, $sce){
>>>
>>> $scope.url = API_URL + ARTICLE_URL + '/' + $routeParams.articleId;
>>>
>>> $http.get($scope.url).then(function(response) {
>>> //console.log(response.data);
>>> $scope.heading = response.data.Headline;
>>> $scope.rawBody = response.data.Body;
>>> $scope.body = $sce.trustAsHtml($scope.rawBody);
>>> $scope.image = response.data.Assets[0].URL;
>>> });
>>> });
>>
>>
>>
>>
>> Unit test:
>>
>> 'use strict';
>>>
>>> describe('Controller: Article', function () {
>>>
>>> var scope,
>>> $httpBackend,
>>> articleEndpoint;
>>>
>>>
>>>
>>> // load the controller's module
>>> beforeEach(module('myApp'));
>>>
>>>
>>> describe('ArticleDetailCtrl', function () {
>>>
>>> var ArticleDetailCtrl,
>>> jsonObject,
>>> ArticleId = '123';
>>>
>>> // Initialize the controller and a mock scope
>>> beforeEach(inject(function ($controller, $rootScope,
>>> _$httpBackend_, Article, API_URL, ARTICLE_URL) {
>>>
>>> scope = $rootScope.$new();
>>> ArticleDetailCtrl = $controller('ArticleDetailCtrl', {
>>> $scope: scope });
>>> $httpBackend = _$httpBackend_;
>>> articleEndpoint = API_URL + ARTICLE_URL + '/' +
>>> ArticleId;
>>>
>>> jsonObject = {
>>> 'Headline': 'Headline',
>>> 'Body': '<p>Body</p>',
>>> 'Assets': [
>>> {
>>> 'URL': 'path/to/image/article1.jpg'
>>> }
>>> ]
>>> };
>>>
>>> $httpBackend.when('GET',
>>> articleEndpoint).respond(jsonObject);
>>> }));
>>>
>>> afterEach(function() {
>>> $httpBackend.verifyNoOutstandingExpectation();
>>> $httpBackend.verifyNoOutstandingRequest();
>>> });
>>>
>>> it('should fetch article details from the API', function () {
>>> //expect(scope.articles.length).toEqual(3);
>>>
>>> $httpBackend.expectGET(articleEndpoint);
>>> $httpBackend.flush();
>>> });
>>>
>>> });
>>>
>>> });
>>
>>
>>
>> But I keep on getting the following error:
>>
>>
>> Error: Unexpected request: GET
>>> http://localhost:3000/api/articles/undefined
>>> Expected GET http://localhost:3000/api/articles/123
>>> at $httpBackend
>>> (/Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1179)
>>> at sendReq
>>> (/Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:8181)
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:7921
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:11319
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:11405
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:12412
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:12224
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1438
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/test/spec/controllers/article.js:77
>>> Error: Unsatisfied requests: GET http://localhost:3000/api/articles/123
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1472
>>> at
>>> /Users/gill/Documents/projects/angularjs-test/test/spec/controllers/article.js:65
>>
>>
>>
>> This is the first time am writing unit tests which I followed along by
>> reading some tutorials. I don't know what am I doing wrong?
>>
>
--
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.