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' })
Written by Tim Anema
Related protips
1 Response
I don't get it, where's the slim?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#