Skip to content

Instantly share code, notes, and snippets.

@davemo
Last active December 16, 2015 07:09
Show Gist options
  • Save davemo/5396706 to your computer and use it in GitHub Desktop.
Save davemo/5396706 to your computer and use it in GitHub Desktop.
Setting up an angular controller (or $scope) test in coffeescript using jasmine-given (you'll need angular-mocks.js for "inject" and "module" to be available to your specs)
app.controller 'SidebarController', ($scope, SidebarPanels) ->
$scope.currentPanel = ''
$scope.panels = SidebarPanels
$scope.setCurrentPanel = (panel) ->
$scope.currentPanel = panel.class
$scope.isCurrentPanel = (panel) ->
$scope.currentPanel is panel.class
describe "SidebarController", ->
Given -> module('app')
Given ->
inject ($controller, $rootScope, SidebarPanels) =>
@scope = $rootScope.$new()
$controller('SidebarController', {$scope: @scope, SidebarPanels: SidebarPanels})
Given -> @panel = {class: 'bar'}
describe "setting the current panel", ->
When -> @scope.setCurrentPanel(@panel)
Then -> expect(@scope.currentPanel).toBe(@panel.class)
describe "checking if a panel is the current panel", ->
context "when the currentPanel is set", ->
Given -> @scope.currentPanel = @panel.class
Then -> expect(@scope.isCurrentPanel(@panel)).toBe(true)
context "when the currentPanel is not set", ->
Given -> @scope.currentPanel = ''
Then -> expect(@scope.isCurrentPanel(@panel)).toBe(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment