使用Jasmine进行端到端测试

use*_*401 3 jasmine angularjs

我正在尝试对使用AngularJS编写的应用程序执行一些端到端测试.目前,我在e2e.tests.js中设置了以下测试:

describe('MyApp', function() {
    beforeEach(function() {
        browser().navigateTo('../index.html');
    });

    it('should be true', function() {
        expect(true).toBe(true);
    });
});
Run Code Online (Sandbox Code Playgroud)

奇怪的是,这个基本测试失败了.测试本身就运行了.但结果是:

203ms   browser navigate to '../index.html'
5ms expect undefined toBe true
           http://localhost:81/tests/e2e.tests.js:10:9
           expected true but was undefined
Run Code Online (Sandbox Code Playgroud)

考虑到"真实"是硬编码的,我希望它通过这个测试.我不知道未定义的真实性.我错过了什么?

Sle*_*rph 7

关于Angular的E2E测试框架的事情看起来像Jasmine,但它不是Jasmine.E2E测试代码实际上都是异步的,但它是以巧妙的方式编写的,因此调用看起来很正常.大多数调用创建异步任务和稍后测试的Future对象.Future对象有点像承诺,但它有点不同.它有一个value在它准备就绪时设置的属性,然后它调用一个done函数移动到下一步.在E2E测试中,该expect函数采用Future对象,而不是值.你看到undefined因为expect正在测试future.value,在这种情况下true.value,是未定义的.

尝试使用返回期货可用选择器之一,然后测试结果.像这样的东西:

expect(element("html").text()).toMatch("Our App");
Run Code Online (Sandbox Code Playgroud)

Future对象没有很好地记录,但您应该能够像这样手动创建Future:

var trueFuture = angular.scenario.Future(
    "a true value",            // name that is displayed in the results
    function(callback) {       // "behavior" function
        callback(null, true);  // supposed to call callback with (error, result)
    });
expect(trueFuture).toEqual(true);
Run Code Online (Sandbox Code Playgroud)

如果查看ng-scenario源代码,可以在angular.scenario.matcher函数中看到匹配器测试future.value的位置.