I noticed strange thing while trying to unit test one of my controllers with karma. Here is spec:
it 'should POST new category data to /api/categories with category name', ->
$httpBackend.expectGET '/api/categories'
.respond 200, ''
$httpBackend.expectPOST '/api/categories', {name: 'food' }
.respond 201, ''
scope.name = 'food'
scope.addCategory()
$httpBackend.flush()
and this is my controller:
angular.module 'fmAppApp'
.controller 'CategoriesCtrl', (Category, $scope) ->
$scope.categories = Category.query()
$scope.addCategory = ->
category = new Category { name: $scope.name}
category.$save()
$scope.categories.push category
$scope.name = ''
Category
dependency above is $resource.
So when I comment the line in controller which performs POST request (category.$save()
)
I get this error from karma:
Error: Unsatisfied requests: POST /api/categories
which I find strange because $httpBackend provides method for this kind of assertions, namely $httpBackend.verifyNoOutstandingExpectation
right?
it's described here in angular docs it's said
Verifies that all of the requests defined via the expect api were made. If any of the requests were not made, verifyNoOutstandingExpectation throws an exception.
I see everybody using it, so I also used to put it in afterEach
but now I don't see the point in it anymore since karma throws error anyway if expected request wasn't made and spec fails.
So is it method useless, or it's something I don't understand?
If I haven't made it clear, I didn't call verifyNoOutstandingExpectation and error happens nevertheless.