1

Im new to angular js testing. We are using Karma/Jasmine to test. I am trying to unit test a directive. Essentially I want to make sure that once it is all compiled that element now contains 'id="tf-' ...... I have verified using dump(element); that in fact i am getting the result im after but my test is not testing it how it should.

in my test file i do this:

var $scope,element;
beforeEach(inject(function($rootScope,$compile) {
    $scope = $rootScope.$new();
    element = engular.element('<div tf-swf></div');
    $compile(element)($scope);
    $scope.$digest();
    dump(element);
})); 

describe('tfSwf directive' , function() {
    it('should place element into DOM' , function() {
        expect(element).toContain('<div id="tf');
    });
});  

so here is the issue ... when i run this it fails and i get an error along the lines of:

TypeError: Object [object Object] has no method 'indexOf' at null.<anonymous>

I know it looks wierd that im dumping that elemnt but this is the output:

Object{0: <div tf-swf=""><div id="tf-2403-place"></div></div>, length: 1}

the fact that the inner div exists and has an id that includes a random number is exactly what im after ... i just cant figure out how to find that inner div in my test.

1 Answer 1

1

You're trying to test if the element object contains a substring. You should first convert the element object to a string:

expect('' + element).toContain('<div id="tf');
Sign up to request clarification or add additional context in comments.

2 Comments

this works . thank you . For reference if anyone else sumbles on this question i found that angular.element returns a jquery object so i was able to do this: expect(element.html()).toContain('<div id="tf');
To be exact, it returns a jqLite object, unless jQuery is available, in which case it returns a jQuery object. See docs.angularjs.org/api/ng/function/angular.element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.