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

Sac*_*nth 4 javascript jquery jasmine angularjs protractor

我正在使用Jasmine for AngularJS编写端到端测试.我正在使用Protractor来运行测试.我有以下标记

<ul class="phone-thumbs">
  <li ng-repeat="img in phone.images">
    <img ng-src="{{img}}">
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我想测试一下,对于特定的页面实例,我有四个这样的图像.这是我到目前为止所拥有的

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);
    });
  });
Run Code Online (Sandbox Code Playgroud)

问题是我得到一个错误说

TypeError: Object #<Object> has no method 'length'
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

Nic*_*ray 10

这个问题是AngularJS教程的教程8中的实验部分的一部分.

这是我如何解决它.我采用了不同的方法来计算渲染的图像,而不是直接测试转发器/模型.

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);
});
Run Code Online (Sandbox Code Playgroud)