简体   繁体   English

使用AngularJS中的Jasmine测试元素数量

[英]Test for the number of elements using Jasmine in AngularJS

I am writing an end to end test using Jasmine for AngularJS. 我正在使用Jasmine for AngularJS编写端到端测试。 I am using Protractor to run the test. 我正在使用Protractor来运行测试。 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? 我哪里错了?

This question is part of the Experiments section in tutorial 8 of the AngularJS tutorial. 这个问题是AngularJS教程的教程8中的实验部分的一部分。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM