I am trying to test a controller which is calling a service. When this service has finished its job it is dispatching an event off the root scope with $emit. I am trying to simulate this in my tests, but I am unable to get the event handler to run. Can anyone see what I am doing wrong?
This is a basic version of my tests:
Files = Mocks.Files;
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
$controller("HelpController", { $scope: scope }); // inherit scope from parent
rootScope = $rootScope.$new();
rootScope.profile = {
....
};
$controller("HelpContentEditorController", {
$scope: scope,
$rootScope: rootScope
Files: Files
});
scope.$apply();
}));
it("should save FileResource to API", function() {
spyOn(Files, "saveFileAPI");
scope.saveContent();
scope.$apply();
rootScope.$emit("allFilesUploaded", []);
rootScope.$apply();
expect(Files.saveFileAPI).toHaveBeenCalled();
});
And then inside the controller I am testing
function saveContent() {
var blob = new $window.Blob([JSON.stringify($scope.content)], {type: "text/json"});
blob.name = "help.json";
$rootScope.$on("allFilesUploaded", onFilesUploaded);
Files.upload([blob]);
}
function onFilesUploaded(event, files) {
Files.saveFileAPI(files[0], files[0].extensions[0]).then(function(data) {
...
}, function (error) {
...
});
}
The test runs without errors, but the onFilesUploaded method is never called, so it fails. I am not sure if it something to do with the way I am inheriting scope, etc but I have tried a few alternatives
Thanks!
Aucun commentaire:
Enregistrer un commentaire