Resolve promise From controller to Jasmine2

189 views
Skip to first unread message

ajay konda

unread,
Dec 11, 2018, 11:46:13 AM12/11/18
to Jasmine
Hi,
I am trying to resolve promise  controller to Jasmine. I have tried with spyOn().and.callThrough() but promise is not resolved. But I am getting data in controller from service. Please let me know where i am making mistake. 
Once I use "scope.$digest()" it shows error "Unexpected request: GET jsonplaceholder.typicode.com/todos/1 No more request expected error properties: Object({ $$passToExceptionHandler: true })". If I remove it test case is passing but console log is not printing. var tee1 shows status: 0;

Can anyone please help.


Code:
spec.js

      beforeEach(inject(function ($controller, $rootScope, _myService_, _$q_, _$rootScope_, _$httpBackend_, $http) {


         scope = $rootScope.$new();
         $q = _$q_;
         $httpBackend = _$httpBackend_;
         $rootScope = _$rootScope_;
         myService = _myService_;
$http = $http;
         ctrl =  $controller('Ctrl', { '$scope': scope, 'myService': myService });
     spyOn(myService, "gettest1").and.callThrough();
})); it('getDateRangeData return Data obj', function(done) {
// var testData = myService.getTestData();
// var promise = myService.getSomething();
var tee = myService.gettest1();

var tee1 = myService.gettest1().then(function(response) {
console.log('Success', response);
}, function() {
console.log("error on chart list");
}) scope.$digest() });

service jsservice js
function gettest1(obj) {
   return $http({
      method: 'GET',
       headers: {
       'Content-Type': 'application/json'
      },
      data: obj
         
    })
}

Gregg Van Hove

unread,
Dec 11, 2018, 8:36:26 PM12/11/18
to jasmi...@googlegroups.com
I'm guessing here a bit, but it looks like you're using Angular and some of the Angular ecosystems various testing helpers. It looks like the Angular http mocking is throwing an error that your AJAX request hasn't been setup in the test. These Angular docs might help: https://angular.io/guide/testing#testing-http-services. I would also take a look at Jasmine's async tutorials (https://jasmine.github.io/tutorials/async) if your your specs aren't running the way you expect.

Hope this helps. Thanks for using Jasmine!

- Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

aja...@sigmoidanalytics.com

unread,
Dec 12, 2018, 12:50:40 AM12/12/18
to Jasmine
Hi Gregg,
Thanks for help. I'm using $httpBackend to mock service. I am using AngulaJS. There an issue while calling function inside promise resolve. I have seen   async tutorial still it not helped. calling function is also another promise it has. please let me know how to use $hhtpBackend flush(); with timeout. If there is one resolve http then it work fine resole inside if i given more function it throws error.
this is first function
  it('getDateRangeData return Data obj', inject(function($q, $rootScope, $http) {

      "userId": 1,
      "id": 1,
      "title": "delectus aut autem",
      "completed": false
    })

    myService.gettest1().then(function() { 
      console.log("success2"); 
    });

    $httpBackend.flush();
      
  }));

it works fine if  in controllercode is like this
function init(){
myService.gettest1().then(function() { 
console.log("i'm working");
$scope.Text = "Welcome"
// loadOtherFunction()
});
}
init();
function loadOtherFunction() {
myService.gettest2().then(function() { 
some code
});
}

If i uncomment loadOtherFunction it throws error and if I consle scope.Text in jasmine it  shows scope.Text undefined.Can I know is it fall in async calling tried with timeout in $httpBackend .flush but still not working. Can you please help me on it.

Gregg Van Hove

unread,
Dec 13, 2018, 8:35:58 PM12/13/18
to jasmi...@googlegroups.com
Unfortunately, I haven't worked that much with the Angular mocking and test libraries, so I'm not sure I personally am going to be of much help here.

Sorry.

-Gregg
Reply all
Reply to author
Forward
0 new messages