Skip to content

Instantly share code, notes, and snippets.

@volkovasystems
Created February 10, 2015 09:18
Show Gist options
  • Save volkovasystems/a1cf3b3d440710516ffe to your computer and use it in GitHub Desktop.
Save volkovasystems/a1cf3b3d440710516ffe to your computer and use it in GitHub Desktop.
Testing Dependency Injected Functions
//Let's assume this is inside a qunit setup function.
//First we create our environment
var window = {
//Pass all global variables here.
//You can also attach mocks here.
"angular": {
"forEach": _.each
}
}
//The extracted function will be stored like this.
var sidebarController = "var sidebarController = function( $scope, $attrs ){..";
//This will eval our function and attach it to window.
vm.runInNewContext( sidebarController, window );
//This will be the scope inside the function the "this"
//This will let us inspect if it really attaches the functions needed.
var scope = { };
//Extract out the function from the window but bind the scope first.
this.sidebarController = window.sidebarController.bind( scope );
//Store the scope for later inspection in the test cases.
this.scope = scope;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment