2013-04-18 137 views
4

我想測試我的角度應用與Yeoman使用摩卡與幻影和柴斷言。 但是,當我運行任何示例測試用例測試用例不能正常運行它顯示PhantomJs超時由於缺少摩卡運行()調用。非角度案件在測試案例中工作正常。測試角度與摩卡和柴

<!doctype html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
    <title>Mocha Spec Runner</title> 
    <link rel="stylesheet" href="lib/mocha/mocha.css"> 
</head> 
<body> 
    <div id="mocha"></div> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script> 
    <script src="lib/mocha/mocha.js"></script> 
    <script>mocha.setup('bdd')</script> 
    <script src="lib/chai.js"></script> 
    <script> 
     expect = chai.expect; 
     assert = chai.assert; 
    </script> 
    <script> 
     function addSum(num1, num2) { 
      return num1 + num2; 
     } 
    </script> 
    <script> 
    (function() { 
    describe('Give it some context', function() { 
     it('should simulate promise', inject(function ($q, $rootScope) { 
      assert.notStrictEqual(3, '3', 'no coercion for strict equality'); 
     /* var deferred = $q.defer(); 
      var promise = deferred.promise; 
      var resolvedValue; 

      promise.then(function(value) { resolvedValue = value; }); 
      expect(resolvedValue).to.be.undefined; 

      // Simulate resolving of promise 
      deferred.resolve(123); 
      // Note that the 'then' function does not get called synchronously. 
      // This is because we want the promise API to always be async, whether or not 
      // it got called synchronously or asynchronously. 
      expect(resolvedValue).to.be.undefined 

      // Propagate promise resolution to 'then' functions using $apply(). 
      $rootScope.$apply(); 
      expect(resolvedValue).to.equal(123);*/ 
     })); 
    }); 
    })(); 
    </script> 



    <!-- trigger the mocha runner --> 
    <script src="runner/mocha.js"></script> 

</body> 
</html> 

回答

2

您是否嘗試過使用量角器?它是專門爲測試端到端angularjs應用程序而開發的(由angularjs團隊開發)。 https://github.com/angular/protractor

它有它自己的亞軍,其中安裝有:

npm install protractor -g 

,然後跑步者與執行:

protractor /configfile.cfg 

無需HTML頁面來運行測試。

配置文件非常簡單(您可以在源代碼中看到這些選項)。

就這樣,你就會有規範定義爲:

// myTest.js 
describe('angularjs homepage', function() { 
    it('should greet the named user', function() { 
     browser.get('http://www.angularjs.org'); 
     element(by.model('yourName')).sendKeys('Julie'); 

     var greeting = element(by.binding('yourName')); 

     expect(greeting.getText()).toEqual('Hello Julie!'); 
     }); 
});