Reputation: 8703
Real simple, I have a controller that has a property set on the scope from a service.
Controller:
dadApp.controller('landingController', function($scope, automationService) {
$scope.hello = "from controller";
$scope.foo = automationService.foo;
// load data from service.
});
Service:
dadApp.factory('automationService', function($http) {
var _foo = [];
$http.get('/api/automation')
.then(function(result) {
angular.copy(result.data, _foo);
},
function() {
alert('error');
});
return {
foo: _foo
};
});
In my specs/test file, how do I test this controller since it depends on a service? (FYI, the service is an ASP.NET WebAPI 2.
Here's my specs:
/// <reference path="../Scripts/angular.js" />
/// <reference path="../Scripts/angular-mocks.js" />
/// <reference path="../Scripts/angular-resource.js" />
/// <reference path="../Scripts/angular-route.js" />
/// <reference path="../app/app.js" />
/// <reference path="../app/services/automationService.js" />
/// <reference path="../app/controllers/landingController.js" />
'use strict';
var scope, ctrl;
describe('DAD tests', function() {
beforeEach(module('dadApp'));
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
ctrl = $controller('landingController', { $scope: scope });
}));
describe('landing controller tests', function() {
describe('scope.hello', function() {
it('should equal "from controller"', function() {
expect(scope.hello).toEqual("from controller");
});
});
});
});
Upvotes: 1
Views: 73
Reputation: 7292
Maybe something like this:
var mockedFoo = {
bar: true,
baz: 123,
other: "string"
};
var automationServiceMock = {
foo: function() {
return mockedFoo;
}
};
describe('DAD tests', function() {
beforeEach(module('dadApp'));
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
ctrl = $controller('landingController', {
$scope: scope,
automationService: automationServiceMock
});
}));
describe('landing controller tests', function() {
describe('scope.hello', function() {
it('should equal "from controller"', function() {
expect(scope.hello).toEqual("from controller");
});
});
});
});
Upvotes: 1