0

I have spent way too much time on this. I have a simple angular app that gets some JSONP from a remote server and displays that on the screen.

I am having the hardest time creating a unit test for it with Jasmine.

Here is my App

var pdApp = angular.module('pdApp', []);
pdApp.controller('pdController', function ($scope, $http) {

  var jsonpUrl = "http://10.1.20.377/products/1/167?cb=JSON_CALLBACK";


  $http.jsonp(jsonpUrl).success(function(data) {

    $scope.pageContent = data.pageContent;
    $scope.cartContent = data.cartContent;
    $scope.content = data.productContent;

  });

});

And here is my unit test

describe('myTest', function () {

  var MainCtrl, scope, httpBackend;

  beforeEach(module('pdApp'));

  module('pdApp', function($provide) {
    $provide.value('DefaultContent', defaultJSON);
  });

  beforeEach(inject(function($controller, $rootScope, $httpBackend) {
    httpBackend = $httpBackend;
    scope = $rootScope.$new();
    MainCtrl = $controller('pdController', {
        $scope: scope
    });
  }));


   it("Product name should be set", function(){
     httpBackend.whenJSONP("/products/1/167").respond(
        {
            "name" : "Prod Name",
            "desc" : "Long Description Here"

        }
     );
     expect(scope.pageContent.name).toEqual("Prod Name");
   });


});

1 Answer 1

1

I notice that you forgot the httpBackend.flush();, so the response will not flush out.

Sign up to request clarification or add additional context in comments.

1 Comment

@bencripps From the document: "the mock $httpBackend has a flush() method, which allows the test to explicitly flush pending requests. This preserves the async api of the backend, while allowing the test to execute synchronously."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.