Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for the number of elements using Jasmine in AngularJS

I am writing an end to end test using Jasmine for AngularJS. I am using Protractor to run the test. I have the following markup

<ul class="phone-thumbs">
  <li ng-repeat="img in phone.images">
    <img ng-src="{{img}}">
  </li>
</ul>

And I want to test that for a particular page instance I have four of these images. Here's what I have so far

describe('Phone detail view', function() {

    beforeEach(function() {
      browser.get('app/index.html#/phones/nexus-s');
    });

    it('should display four thumbnails on the nexus-s page', function() {
      expect(element(by.css('.phone-thumbs li img')).length()).toBe(4);
    });
  });

Problem is that I get an error saying

TypeError: Object #<Object> has no method 'length'

Where am I going wrong?

like image 204
Sachin Kainth Avatar asked May 10 '14 16:05

Sachin Kainth


People also ask

What is Jasmine in AngularJS?

Jasmine is a behavior driven development framework for JavaScript that has become the most popular choice for testing AngularJS applications. Jasmine provides functions to help with structuring your tests and also making assertions.

How do I run a test case in AngularJS?

Testing in AngularJS is achieved by using the karma framework, a framework which has been developed by Google itself. The karma framework is installed using the node package manager. The key modules which are required to be installed for basic testing are karma, karma-chrome-launcher ,karma-jasmine, and karma-cli.

Which testing framework should I use for angular testing?

Similar to Karma, it’s also the recommended testing framework within the Angular documentation as it’s setup for you with the Angular CLI. Jasmine is also dependency free and doesn’t require a DOM. As far as features go, I love that Jasmine has almost everything I need for testing built into it.

How to test a function in Jasmine?

We can test this by using the Jasmine function expect (), which will verify whether the incoming output is equal, contain, toBe an expected output or not. It is going to help us to assert that the functionality returns the given output. Finally, our spec file will look as follows: To run the spec file we need to write the command ng test.

What is the difference between Karma testing and unit testing in AngularJS?

In AngularJS, we can perform Unit Testing separately for controllers and directives. We can also perform end of end testing of AngularJS, which is testing from a user perspective. Karma is a testing automation tool created by the Angular JS team at Google.

How to use testbed in Angular 2+?

In the beforeEach function for our test suite, we configure a testing module using the TestBed class. This creates a test Angular Module which we can use to instantiate components, perform dependency injection, and so on.The testBed is a mock environment to run Angular 2+ component tests without the browser.


1 Answers

This question is part of the Experiments section in tutorial 8 of the AngularJS tutorial.

Here is how I solved it. I took the different road of counting the images rendered rather than testing the repeater/model directly.

it('should display four thumbnails of the nexus-s', function() {
  var imgsCount = element.all(by.css('.phone-thumbs li img')).count();
  expect(imgsCount).toBe(4);
});
like image 87
Nicholas Murray Avatar answered Nov 02 '22 23:11

Nicholas Murray