简体   繁体   中英

Testing Angular With Mocha and Chai

I want to test my angular app with Yeoman which use Mocha with Phantom and Chai for assertion. But when i run any sample test case the test case do not run properly it shows PhantomJs timed out due to missing Mocha run() call.Non angular Cases are working fine in test case.

<!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>

Have you tried using protractor? It has been developed specifically for testing end to end angularjs apps (by the angularjs team). https://github.com/angular/protractor

It has it's own runner, which you install with:

npm install protractor -g

and then the runner is executed with:

protractor /configfile.cfg

No need for an HTML page to run the tests.

The config file is quite simple (you can see the options in the source code).

With that, you'll have the spec defined as:

// 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!');
      });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM