title: Why unit testing response is always successful?
body: <p>I was reading posts related for don't repeat the question.</p>
  
  <p>I have the next unit testing code:</p>
  
  <pre><code>describe('service', function() {
  var questionApiService;
  
  beforeEach(module('myApp'));
  
  beforeEach(inject(function (_questionApiService_) {
      questionApiService = _questionApiService_;
  }));
  
  // Test service availability
  it('check the existence of get field question service', inject(function(questionApiService) {
  //expect(1).toEqual(100);
  
      questionApiService.getField()
          .then(function(data) {
  
              //console.log(data);
              expect(1).toEqual(100);
          });
  }));
  </code></pre>
  
  <p>});</p>
  
  <p>If I run the code <code>expect(1).toEqual(100);</code> outside the service, the result is Error, but if I write the same code <code>expect(1).toEqual(100);</code> inside the service, the result is Success, which makes me think that the validator is not entering the service.</p>
  
  <p>Whats wrong?</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>Hello Asta, I think ur idea is very good and i'm trying to implement it. I have an error in my code and i don't know how do debugging:</p>
  
  <pre><code>defer = $q.defer();
  spyOn(questionApiService, 'getField').andReturn(defer.promise);
  defer.resolve(data);
  expect(data.nextQ).toEqual(1);
  </code></pre>
  
  <p>My unit testing always fails. If promise is successful, the "data" object must have nextQ attribute. </p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Hi Asta, your code is amazing. I'm trying to execute your code in my system and still with error. The ut fails:</p>
  
  <p>Error: Unexpected request: GET <a href="http://mi.url.com/api/thefield" rel="nofollow">http://mi.url.com/api/thefield</a>   No more request expected</p>
  
  <p>Do u know what's wrong? Clarify that the code works fine on my application but ut is the problem.</p>
  
  <p>Question Api Service code:</p>
  
  <pre><code>angular.module('myApp.services')
      .factory('questionApiService', function($http, $q) {
          var myService = {
              getField: function() {
                  var defer = $q.defer();
  
                  $http.get('http://mi.url.com/api/thefield')
                  .success( function(data) {
                      defer.resolve(data);
                  })
                  .error( function(data) {
                      defer.reject(data);
                  });
  
                  return defer.promise;
              };
  
          return myService;
      });
  </code></pre>
  
  <p>Your test:</p>
  
  <pre><code>describe('myApp', function () {
  
      beforeEach(function () {
          module('myApp');
      });
  
      describe('questionApiService', function () {
          it('should check the existence of get field question service', inject(function($rootScope, questionApiService) {
              var response = null;
              var promise = questionApiService.getField();
  
              promise.then(function(data) {
                  response = data;
              });
  
              $rootScope.$apply();
  
              var expectedResponse = { "nextQ": 1 };
              console.log(response);
              //expect(JSON.parse(response.nextQ)).toEqual(expectedResponse.nextQ);
          }));
  
      });
  });
  </code></pre>