Class: EasyTest

EasyTest

new EasyTest()

Source:

Methods

<static> compileDirective(directiveHTML, scope, parent) → {HTMLElement}

Compiles a directive and returns the top level element in the compiled directive's HTML. Note that currently you need to mock any modules that this directive may need beforehand. See the EasyTest.mockModule function.
Parameters:
Name Type Description
directiveHTML string The HTML of the directive to be compiled.
scope object a dictionary of variables that will be inserted into the element's scope.
parent HTMLElement | angular.element an element to attach the compiled directive as a child. If scope is an HTMLElement or an Angular element, it will be treated as the parent.
Source:
Returns:
The top level HTML element of the compiled directive.
Type
HTMLElement
Examples
var element = EasyTest.compileDirective('<p directive></p>');
expect(element.length).to.equal(expectedLength);
var scope = { a: 1, b: 'something' };
var element = EasyTest.compileDirective('<p>{{a}} {{b}}</p>', scope);
expect(element.text()).to.equal('1 something');
var scope = { foo: 'bar' };
var parent = angular.element('<div></div>');
var element = EasyTest.compileDirective('<p>{{foo}}</p>', scope, parent);
expect(angular.element(parent.children()[0]).text()).to.equal('bar');
var parent = angular.element('<div></div>');
var element = EasyTest.compileDirective('<p>baz</p>', parent);
expect(angular.element(parent.children()[0]).text()).to.equal('baz');

<static> createTestContext(controller) → {object}

Creates a 'test context' for a particular controller. Note that currently you need to mock any modules that this controller may need beforehand. See the EasyTest.mockModule function.
Parameters:
Name Type Description
controller string The name of the controller to load and create a context for.
Source:
Returns:
An object with a $scope and controller property representing the controller that has been loaded and its scope.
Type
object
Example
var context = EasyTest.createTextContext('MyControllerName');
expect(context.$scope).to.have.property('MyExpectedProperty');
expect(context.controller.myFunc).to.be.a('function');

<static> getBoundController(controllerName, scope) → {object}

Gets a specific controller and returns a reference to it. Use this instead of `getController` if the controller belongs to a directive that has `bindToController` set to true.
Parameters:
Name Type Description
controllerName string The name of the controller to get.
scope object a object that will be inserted into the controller's scope.
Source:
Returns:
The controller that was retreived.
Type
object
Example
var scope = { value: 'some value' };
var myController = EasyTest.getBoundController('myController', scope);

<static> getBoundScope(angularElement) → {object}

Allows access to the controller scope for directive that has `bindToController` set to true.
Parameters:
Name Type Description
angularElement object An angular element gotten from compiled HTML.
Source:
Returns:
The scope of the bound controller.
Type
object

<static> getController(controllerName) → {object}

Gets a specific controller and returns a reference to it.
Parameters:
Name Type Description
controllerName string The name of the controller to get.
Source:
Returns:
The controller that was retreived.
Type
object
Example
var myController = EasyTest.getController('myController');

<static> getService(serviceName) → {object}

Injects a specific service and returns a reference to it.
Parameters:
Name Type Description
serviceName string The name of the service to inject.
Source:
Returns:
The service that was injected.
Type
object
Example
var $q = EasyTest.getService('$q');

<static> hasAttr(element, attr, val) → {boolean}

Tests an HTMLElement or Angular/jQuery element to see if it contains some attribute, optionally with some given value.
Parameters:
Name Type Description
element HTMLElement | angular.element the element to be tested.
attr string the name of the attribute being tested.
val string optional: the value of the attribute being tested.
Source:
Returns:
Whether the element contains the attribute `attr`. If `val` is provided, returns true if and only if the value of `attr` is also `val`.
Type
boolean
Example
var element = EasyTest.compileDirective('<p foo="bar"></p>');
expect(EasyTest.hasAttr(element[0], 'foo')).to.be.true;
expect(EasyTest.hasAttr(element[0], 'foo', 'bar')).to.be.true;

<static> injectify(injectibles) → {object}

Injects a number of services and returns an object that has, as its properties, the injected services.
Parameters:
Name Type Description
injectibles array An array of the names of the services to inject.
Source:
Returns:
An object that has, as its properties, the services that were injected.
Type
object
Example
var services = EasyTest.injectify(['MyServiceOne', 'MyServiceTwo']);
expect(services.MyServiceOne).to.be.an.instanceOf(MyServiceOne);

<static> looksLike(object, spec) → {object}

Tests an object against a JSON specification object.
Parameters:
Name Type Description
object object The object to test.
spec object The 'specification' to test the object against. Should have the expected types as properties and the names of the properties with that type as their value, separated by spaces.
Source:
Returns:
An error object if anything failed, nothing if it passed.
Type
object
Example
// Using Chai you could pass the result directly back to 'done'
it('test object', function(done) {
  var res = EasyTest.looksLike(object, {
    'function': 'funcOne funcTwo',
    'number': 'numberOne numberTwo numberThree'
  });
  done(res);
});

<static> mockModule(moduleName, services)

Mocks a specified module that has already been registered with angular. Optionally registers a number of services with the mocked module.
Parameters:
Name Type Description
moduleName string The name of the module to mock.
services array | object An array of objects representing fake services to register with the module. Each object must have a `name` property representing the name of the service, and then one of `provider`, `constant`, `value`, `factory`, or `service`. Alternately, you can supply an object matching names to factories as this is typically the most default use case.
Source:
Examples
// Mock MyModule and provide the passed fake services to it.
EasyTest.mockModule('MyModule', [{
  name: 'ServiceName',
  factory: function() {}
},{
  name: 'MyFakeConstant',
  constant: 1337
}]);
// Mock MyModule and provide the passed fake services to it.
EasyTest.mockModule('MyOtherModule', {
  FooFactory: { foo: function() {} },
  BarFactory: { bar: function() {} }
});

<static> mockModules(arguments)

Mocks a series of modules. The arguments that are passed into this function can be passed in a very flexible manner. String arguments will be split on their spaces and mocked directly, object arguments will be expected to have a `name` property and `values` property which is an array of the values to be provided for the module. See the EasyTest.mockModule function for more information on how that works.
Parameters:
Name Type Description
arguments string | object A series of string or object parameters.
Source:
Examples
// Simply mock three modules
EasyTest.mockModules('one two three');
// or
EasyTest.mockModules('one two', 'three');
// etc, can mix and match.
// Mock two simple modules and a third with a fake service.
EasyTest.mockModules('one two', { name: 'three', values: [{
  name: 'FakeService',
  service: { get: function() {} }
}]});

<static> testController(controller, spec)

Tests a controller to see if it conforms to a JSON specification object. See EasyTest.looksLike for more information.
Parameters:
Name Type Description
controller string | object The name of the controller to create or a reference to a controller.
spec array The controller's spec. An object with the names of the expected types as its keys and a string for its value of all the properties, separated by spaces.
Source:
Example
// Will load the controller and test it using the passed spec.
EasyTest.testController('MyControllerName', {
  'function': 'funcOne funcTwo funcThree',
  'number': 'numberOne numberTwo',
  'boolean': 'hasStuff'
});

<static> testScope(context, scopeSpec)

Tests a controller's scope by seeing if it conforms to a JSON specficiation object. See EasyTest.looksLike for more information.
Parameters:
Name Type Description
context string | object The name of the controller to create a test context for or the context itself.
scopeSpec array The scope's spec. An object with the names of the expected types as its keys and a string for its value of all the properties, separated by spaces.
Source:
Example
// Will load the controller and test it using the passed spec.
EasyTest.testScope('MyControllerName', {
  'function': 'funcOne funcTwo funcThree',
  'number': 'numberOne numberTwo',
  'boolean': 'hasStuff'
});

<static> testService(service, spec)

Tests a service to see if it conforms to a JSON specification object. See EasyTest.looksLike for more information.
Parameters:
Name Type Description
service string | object The name of the service to create or a reference to a service.
spec array The service's spec. An object with the names of the expected types as its keys and a string for its value of all the properties, separated by spaces.
Source:
Example
// Will load the service and test it using the passed spec.
EasyTest.testService('MyServiceName', {
  'function': 'funcOne funcTwo funcThree',
  'number': 'numberOne numberTwo',
  'boolean': 'hasStuff'
});