Last Updated: February 25, 2016
·
1.211K
· tanema

Testing Angularjs Directives with Testacular/Karma

There were some question on how to test directives if I am doing my templates with slim as I explained in my last "pro tip". I use Karma (formerly Testacular) and have done it as follows. Here is simple example of my linkto directive. (using jasmine)

describe 'link_to', () ->
  subject = null
  elem = null

  beforeEach(module('MyApp'))
  beforeEach inject ($rootScope, $compile) ->
    subject = $rootScope.$new()
    elem = angular.element('<div linkto="/locations/abc" search="{dietary_issues: \'peanuts,gluten\'}"></div>')
    $compile(elem)(subject)

  it "should call scope.apply", () ->
    spyOn(subject, "$apply")
    elem.click()
    expect(subject.$apply).toHaveBeenCalled()

  it "should call the main helper link_to with correct params", () ->
    spyOn(subject, "link_to")
    elem.click()
    expect(subject.link_to).toHaveBeenCalledWith('/locations/abc', { dietary_issues : 'peanuts,gluten' })

1 Response
Add your response

I don't get it, where's the slim?

over 1 year ago ·