首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试angular app的karma e2e如何设置jasmine?

测试angular app的karma e2e如何设置jasmine?
EN

Stack Overflow用户
提问于 2013-06-03 16:28:19
回答 1查看 3.6K关注 0票数 1

我尝试使用karma和jasmine创建e2e测试。在我的karma-e2e.conf.js中,我添加了jasmine:

代码语言:javascript
复制
files = [
   JASMINE,
   JASMINE_ADAPTER,
   ANGULAR_SCENARIO,
   ANGULAR_SCENARIO_ADAPTER,
   'test/e2e/**/*.js'
];

A需要异步测试,所以我需要使用runswaitswaitsFor (https://github.com/pivotal/jasmine/wiki/Asynchronous-specs)

但如果我试着使用它:

代码语言:javascript
复制
it('test', function () {
    runs(function () {
        ...
    });
});

Scenatio测试运行程序返回以下内容:

代码语言:javascript
复制
TypeError: Cannot call method 'runs' of null
    at runs (http://localhost:8080/adapter/lib/jasmine.js:562:32)
    at Object.<anonymous> (http://localhost:8080/base/test/e2e/eduUser.js:42:3)
    at Object.angular.scenario.SpecRunner.run   (http://localhost:8080/adapter/lib/angular-scenario.js:27057:15)
    at Object.run (http://localhost:8080/adapter/lib/angular-scenario.js:10169:18)

我不知道问题出在哪里。你能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-03 20:04:32

Karma的Angular e2e测试不能也不能使用JASMINE适配器。取而代之的是ANGULAR_SCENARIO_ADAPTER,它的感觉类似于编写Jasmine测试。

适配器的API中的所有命令都是异步的。例如,element('#nav-items').count()不返回数字,它返回一个Future对象。Future对象被放在一个队列中,并随着运行进程的进行而异步执行。引用API docs

expect(future).{matcher}:

..。所有API语句都会返回一个future对象,该对象在执行后会获得一个赋值。

如果您需要运行自己的异步测试代码,您可以扩展适配器的DSL,这比听起来更简单。这个想法是你返回你自己的Future,它可以由像toBe()这样的匹配器计算。在the e2e-tests.js Gist from Vojta中有一些关于如何做到这一点的例子。只需记住在测试代码成功时调用done(null, myRetrunValue); (myRetrunValue是匹配器计算的值)。如果希望测试失败,则返回done('Your own error message');

更新:回答下面的问题。要模拟登录,首先向dsl添加一个名为login的函数

代码语言:javascript
复制
angular.scenario.dsl('login', function() {
  return function(selector) {
    
    // @param {DOMWindow} appWindow The window object of the iframe (the application)
    // @param {jQuery} $document jQuery wrapped document of the application
    // @param {function(error, value)} done Callback that should be called when done
    //                                      (will basically call the next item in the queuue)
    return this.addFutureAction('Logging in', function(appWindow, $document, done) {

      // You can do normal jQuery/jqLite stuff here on $document, just call done() when your asynchronous tasks have completed
      
      // Create some kind of listener to handle when your login is complete
      $document.one('loginComplete', function(e){
        done(null, true);
      }).one('loginError', function(e){
        done('Login error', false);
      });
      
      // Simulate the button click
      var loginButton = $document.find(selector || 'button.login');
      loginButton.click();
    })
  };
});

然后打电话给:

代码语言:javascript
复制
beforeEach( function()
{
    expect( login('button.login') ).toBeTruthy();
});
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16892697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档