Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevermeister/014339d4f8630c1537d3cdf4fcf30d13 to your computer and use it in GitHub Desktop.
Save stevermeister/014339d4f8630c1537d3cdf4fcf30d13 to your computer and use it in GitHub Desktop.
app-test
describe('Calc', function() {
beforeEach(module('myApp'));
describe('service', function() {
it('should do sum', inject(function(Calc) {
expect(Calc.sum(3,4)).toBe(7);
}));
});
describe('controller', function() {
var controller, Calc;
beforeEach(inject(function($controller, _Calc_) {
Calc = _Calc_;
spyOn(Calc, 'sum').and.returnValue(7);
controller = $controller('MainController', { Calc: Calc});
}));
it('should call Calc service', function() {
controller.doCalculations(3, 4);
expect(Calc.sum).toHaveBeenCalled();
});
it('should set result', function() {
controller.doCalculations(3, 4);
expect(controller.result).toBe(7);
});
});
});
@stevermeister
Copy link
Author

angular.module('myApp', []);

angular.module('myApp')
  .service('Calc', function() {
    this.sum = function(a, b) {
      return a + b;
    }
  });

angular.module('myApp')
  .controller('MainController', function(Calc) {

    this.doCalculations = function(a, b) {
      this.result = Calc.sum(a, b);
    }

  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment