2

Here, _fact is a reference to the service.

 it('Git Check', function() {
     $scope.user = 'swayams'
     var data;
      _fact.Git($scope).then(function(d) {
        expect(d.data.length).toEqual(4)
     }, function() {  expect(d).not.toBeNull(); });

    });

I am getting the error

SPEC HAS NO EXPECTATIONS Git Check

Update

After forcing async as per @FelisCatus and adding $formDigest, I am getting a different error Error: Unexpected request: GET https://api.github.com/users/swayams/repos No more request expected

The updated code snippet looks something like -

 it('Git Check', function(done) {
         $scope.user = 'swayams'
         var data;
          _fact.Git($scope).then(function(d) {
            expect(d.data.length).toEqual(4)
         }, function() {  expect(d).not.toBeNull(); });

        });
 $rootScope.$formDigest();

I have a Plunk here illustrating the issue.

Swayam Siddha
  • 245
  • 1
  • 3
  • 16

1 Answers1

0

Jasmine is not seeing your expectations because your function returns before any expect() is called. Depending on your situation, you may want to use async tests, or use some promise matchers.

With async tests, you add an additional argument to your test function, done.

it('Git Check', function (done) {
  $scope.user = 'swayams'
  var data;
   _fact.Git($scope).then(function(d) {
     expect(d.data.length).toEqual(4);
  }, function() {  expect(d).not.toBeNull(); }).finally(done);
  $rootScope.$digest();
});

(Note the finally clause in the end of the promise chain.)

Please note that you have to do $rootScope.$digest() for the promises to resolve, even if your code is not using it. See: How to resolve promises in AngularJS, Jasmine 2.0 when there is no $scope to force a digest?

Community
  • 1
  • 1
FelisCatus
  • 5,054
  • 2
  • 21
  • 25
  • I tried to implement as you have mentioned here. Got an error `Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.` – Swayam Siddha Sep 07 '15 at 10:43
  • Well, at least it is an async test now. I think the issue now is related to the promise not being resolved. Either it takes too long, or you forget to call $digest or something to make the promise complete. – FelisCatus Sep 07 '15 at 10:56
  • Can you just try a simple `$timeout(30).then(function(){ expect(1+1).toEqual(2); }).finally(done)` instead? By doing so you can locate the issue in either the async and promise part, or something in your `_fact.Git`. – FelisCatus Sep 07 '15 at 10:58
  • The $timeout.. part still triggers the same error as my service function. I have a controller where the `_fact.Git` is being used and it works fine. – Swayam Siddha Sep 07 '15 at 11:05
  • Does `$timeout(done)` works? How about a straightforward `done()` call? – FelisCatus Sep 07 '15 at 11:14
  • $timeout(done) doesnt work. done() call inside of the `it` block brings us back to the issue described in the question. I dont have anthing around `$digest` in my implementation. I am lookig into that right now – Swayam Siddha Sep 07 '15 at 11:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88995/discussion-between-swayam-siddha-and-feliscatus). – Swayam Siddha Sep 07 '15 at 11:25
  • I've already posted link about `$timeout`, `$rootScript.$digest()` and promises on chat. If you have more questions, please chat me. Meantime, I will try to compose these into an answer while you are AFK. – FelisCatus Sep 07 '15 at 12:25