Test for the number of elements using Jasmine in AngularJS

家住魔仙堡 提交于 2019-12-10 16:16:50

问题


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?


回答1:


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);
});



回答2:


For anyone who needs to know this. Here's how I did it.

it('should display four thumbnails on the nexus-s page', function() {
      var images = element.all(by.repeater('img in phone.images')).count();
      expect(images).toBe(4);
    });


来源:https://stackoverflow.com/questions/23583408/test-for-the-number-of-elements-using-jasmine-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!