angularjs - beforeEach(防止测试污染)未按预期工作

标签 angularjs testing protractor

我一直在做 AngularJS 教程,并且在端到端测试之一的 beforeEach 中遇到了一个奇怪的问题。对于上下文,我目前位于实验部分 step 3 。测试代码在这里:

'use strict';

/* http://docs.angularjs.org/guide/dev_guide.e2e-testing */

describe('PhoneCat App', function() {

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

    beforeEach(function() {
      browser.get('app/index.html');
    });

    it('should filter the phone list as user types into the search box', function() {

      var phoneList = element.all(by.repeater('phone in phones'));
      var query = element(by.model('query'));

      expect(phoneList.count()).toBe(3);

      query.sendKeys('nexus');
      expect(phoneList.count()).toBe(1);

      query.clear();

      query.sendKeys('motorola');
      expect(phoneList.count()).toBe(2);
    });
  });

  it('should display current filter value within an element with id "status"', function() {

    var statusElement = element(by.id('status'));
    expect(statusElement.getText()).toMatch(/Current filter:\s*$/);


    element(by.model('query')).sendKeys('nexus');

    expect(statusElement.getText()).toMatch(/Current filter: nexus\s*$/);

  });
});

beforeEach block 应该在每个规范执行之前重新加载页面,但似乎并非如此,因为当我使用 Protractor 运行此操作时,插入到第一个规范(“motorola”)中的查询元素中的最后一个文本仍然存在执行第二个规范,导致第二个规范失败。

现在,当我将 beforeEach 移动到外部描述 block 时,所有规范都成功通过。有任何想法吗?

最佳答案

glepretre 是正确的。按照上面示例的结构,您的测试如下所示

describe
  describe
    before
    it
  it

因为这种嵌套, before 只在第一个 it 之前运行一次。看看每个 it block 正在测试的内容,我认为它们都旨在测试电话 ListView ,在这种情况下,正确的嵌套如下:

describe
  describe
    before
    it 
    it 

但是,如果索引页面不是特定于电话 ListView 而是包含整个应用程序,那么它可能应该加载到顶级描述 block 中,这使得这是最正确的方法:

describe
  before
  describe
    it
    it

关于angularjs - beforeEach(防止测试污染)未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23304377/

相关文章:

testing - Geb 无法将我的类(class)视为内容页面

ruby-on-rails - 使用 counter_cache 列编写装置

java - 如何使用 dbUnit 将数据库恢复到初始状态?

dom - 如何获取元素的父元素

javascript - AngularJS 自定义指令 templateURL

AngularJs 指令 - 如何从指令中获取属性值

javascript - 为什么 fullstack-angular 生成器使用 Lo-Dash 的合并而不是 document.set?

javascript - 为什么 AngularJS 默认事件指令使用 `compile` 方法而不是 `link` 来读取属性?

angularjs - 在 Visual Studio 2013 中运行 Protractor 测试

testing - 如何在 google.com 上使用 Protractor?