【问题标题】:beforeEach (to prevent test pollution) not working as expectedbeforeEach(以防止测试污染)没有按预期工作
【发布时间】:2014-05-03 11:06:48
【问题描述】:

我一直在学习 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 块应该在每次规范执行之前重新加载页面,但似乎不是,因为当我使用量角器运行它时,插入到第一个规范('motorola')中的查询元素中的最后一个文本仍然存在第二个规范被执行,导致第二个规范失败。

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

【问题讨论】:

  • 也许是我,但我看不出问题出在哪里:“刷新”应该在 main beforeEach (在describe('PhoneCat App') 下)正常工作,即清理每个测试之间的条目。
  • 来自这里的文档 [link] [jasmine.github.io/1.3/… 它应该在每个规范之前的两个地方都有效。

标签: angularjs testing protractor


【解决方案1】:

glepretre 是正确的。如上例所示,您的测试如下所示

describe
  describe
    before
    it
  it

由于这种嵌套, before 只运行一次,在第一个 it 之前。查看每个 it 块正在测试的内容,我认为它们都旨在测试电话列表视图,在这种情况下,正确的嵌套如下:

describe
  describe
    before
    it 
    it 

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

describe
  before
  describe
    it
    it

【讨论】:

    猜你喜欢
    • 2016-08-27
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多