Last active
November 11, 2015 22:20
-
-
Save creaux/b5775129a78cc2d1328e to your computer and use it in GitHub Desktop.
Angular tets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Declare inject controller', function() { | |
// 1. Prerequisities | |
// Variables which are avalaible on parent scope | |
var $controller, | |
ctrl, | |
serviceMock; | |
// 2. We are using module foo because it contains desired controller | |
beforeEach(module('foo')); | |
beforeEach(function() { | |
// 3. Register module | |
// $provide will help you register new services on that module | |
module(function($provide){ | |
// 4. Mock relevant service you want | |
serviceMock = $provide.service('OverridedService', function() { | |
// 5. Here you can register your methods and properties | |
// for later call in your tests | |
this.some = 'foobar'; | |
}); | |
}); | |
}); | |
// 6. Inject $q service | |
beforeEach(inject(function(_$controller_, _OverridedService_) { | |
$controller = _$controller_; | |
ctrl = $controller('NameOfCtrl', { OverridedService: _OverridedService_ }) | |
})); | |
// 7. Is $q service is defined? | |
it('should work', function() { | |
expect(ctrl).toBeDefined(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Declare inject service', function() { | |
// Prerequisities | |
// Create your serviceMock in parent scope | |
// to have accessible it outside of callbacks | |
var foo = ['some', 'content'], | |
// Name of service that we will use in this scope | |
$q; | |
beforeEach(function() { | |
module('foo'); | |
}); | |
// 1. Inject $q service | |
beforeEach(inject(function(_$q_) { | |
$q = _$q_; | |
})); | |
// 2. Is $q service is defined? | |
it('should work', function() { | |
expect($q).toBeDefined(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Declare promise mock', function() { | |
// Prerequisities | |
// Create your serviceMock in parent scope | |
// to have accessible it outside of callbacks | |
var foo = ['some', 'content'], | |
// Declare the service mock that we will use later | |
serviceMock, | |
// Declaration of $q which we will use later | |
$q; | |
// 1. Inject $q service that helps you mock promise | |
beforeEach(inject(function(_$q_) { | |
$q = _$q_; | |
})); | |
// 2. Create service in beforeEach | |
beforeEach(function() { | |
// 3. Load the module on which we will mock method that return promise | |
module('foo'); | |
// 4. $provide provider will help you register new services on that module | |
module(function($provide){ | |
// 5. Mock relevant service you want | |
serviceMock = $provide.service('foobar', function() { | |
// 6. Here you can register your methods that will return promise | |
// for later call in your tests | |
this.foo = $q.when(foo); | |
}); | |
}); | |
}); | |
// 6. This works well | |
// This test means that you can use your service inside of | |
// controller and whatever you want because value is how is expected | |
it('should work', function() { | |
expect(serviceMock.foo).expect(foo); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Declare service mock', function() { | |
// Prerequisities | |
// Create your serviceMock in parent scope | |
// to have accessible it outside of callbacks | |
var serviceMock; | |
// 1. Run before each test | |
beforeEach(function(){ | |
// 2. Load the module on which you want to mock all services | |
// If you are testing module called 'foo' and need to mock | |
// services on 'bar' you can mock them directly on 'foo' | |
// from testing point of view there is no differece between that | |
module('foo'); | |
// 3. $provide provider will help you register new services on that module | |
module(function($provide){ | |
// 4. Mock relevant service you want | |
serviceMock = $provide.service('searchUnitService', function() { | |
// 5. Here you can register your methods and properties | |
// for later call in your tests | |
this.someStaff = 'foobar'; | |
}); | |
}); | |
}); | |
// 6. This works well | |
// This test means that you can use your service inside of | |
// controller and whatever you want because value is how is expected | |
it('should work', function() { | |
expect(serviceMock.some).expect('foobar'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment