Reputation: 19338
I have start to learning how to test properly on controller and encounter this following script.
http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-karma.html
it('should have a properly working VideosCtrl controller', inject(function($rootScope, $controller, $httpBackend) {
var searchTestAtr = 'cars';
var response = $httpBackend.expectJSONP(
'https://gdata.youtube.com/feeds/api/videos?q=' + searchTestAtr + '&v=2&alt=json&callback=JSON_CALLBACK');
response.respond(null);
var $scope = $rootScope.$new();
var ctrl = $controller('VideosCtrl', {
$scope : $scope,
$routeParams : {
q : searchTestAtr
}
});
}));
I am a bit confused... expectedJSON is in the following API page:
https://docs.angularjs.org/api/ngMock/service/$httpBackend
I am confused Just wondering what's testing in here, i can see the describe there but still lost... what's expected here?
Upvotes: 0
Views: 64
Reputation: 38151
This test has no explicit assertions, but it will fail if constructing the controller instance throws an exception for any reason.
Remember that if an assertion is failed, it throws an exception. Test runners just run your test function like so:
try {
runTest();
} catch (e) {
markTestFailed(e);
}
So if your test code throws an exception for any reason, the test will fail.
Upvotes: 2
Reputation: 42669
As with any unit test, you arrange act and then assert.
For the above test, the parts of arrange and act are self evident but the assert part may not be obvious. The test is just verifying that the search request was send when controller was created.
If you look at the controller code for video controller
$youtube.query($scope.q, true, function(q, videos) {
$scope.videos = videos;
$scope.onReady();
});
this can be confirmed.
The assertion part is done by the $httpBackend.expectJSONP
. This setup tells karma to verify that a GET request is made to a specific url. The url here is the google search api with the search term cars
.
The test basically sets up a mock backend and asserts that it was called.
Upvotes: 2