user3597741
user3597741

Reputation: 449

jasmine no pending request to flush POST

so I have the following test in jasmine:

    $httpBackend.whenPOST('/api/userdata/saveuser').respond({ 'result': 'true' })

it('should save the new or updated user', function () {
        var controller = createController();
        $httpBackend.flush();
        spyOn(controller, 'saveUser');

        controller.saveUser();
        $httpBackend.flush();
        expect(controller.saveUser).toHaveBeenCalled();
    })

When I go to perform the FLUSH after the saveUser I get the famous 'no pending requests to flush'. The controller function looks like the following:

  user.saveUser = function () {
          userDataService.saveUser(user.data).then(function (result) {
             $state.go("license", { guid: user.data.user.account });
        });
    };

Of course notice that I am not using $scope anywhere in my controller.

Don't get it!! I tried to put some console.log statements in the controller to see if it was hitting it... and I don't see it going into the procedure. YET the expect(controller.saveUser).toHaveBeenCalled() is successful.

Here is the dataservice:

function saveUser(user) {
            return $http.post("/api/userdata/saveuser", user.data).success(returndata).error(returnerror)
        };

UGGH Why is this happening????

Upvotes: 0

Views: 3398

Answers (1)

HankScorpio
HankScorpio

Reputation: 3651

You've got $httpBackend.flush(); in your test twice. The first one is the one that's likely causing the error. Remove that line. You don't want to flush() until after you've done something that's flushable.

Also, you're spying on the method that you're testing. Remove that line too, or at least change it to:

spyOn(controller, 'saveUser').and.callThrough();

It doesn't actually make sense to spy that method. You know it'll work because your test calls that function directly.

Upvotes: 2

Related Questions