AngularJS & mocha: $httpBackend call before each test - angularjs

I am working with angularjs and writing tests in mochajs.
I implemented multi-language support in my application and now I have the problem, that in many test-cases i get an unexpected request for my language.json
Do you have any idea how to handle this, without adding the "$httpBackend.whenGet(....." for the language-handling to each test?

Both Mocha and Jasmine support root-level hooks (which will affect all describes).
When loaded in the first place in karma, this
angular.module('test-setup', []).run(function ($httpBackend) {
$httpBackend.whenGET(...
});
beforeEach(module('test-setup'));
should provide $httpBackend setup for all specs.

Related

Is it possible to use Jasmine without Karma for testing Angular/Node based Nw.js apps?

I've read ton's of tutorials, but I must admit that this testing stuff is still very confusing to me. I have a Nw.js app which (of course) uses NodeJS and also Angular. I've installed the Jasmine test framework globally via npm and wrote an example test which starts with the following lines, and placed it in the spec sub-directory:
describe ( 'Test for my controller', function () {
beforeEach ( module ('module_under_test') );
... and so on ...
});
When running the test by typing jasmine on the cmd line (from the root folder of the app), I get the following error message:
TypeError: module is not a function
I know that I have to include the Angular library somehow. But where? In a normal browser application, it is included in the HTML <script> tag, but I don't have this possibility. I also know that I could write a HTML file, which shows the Jasmine result page after tests have finished, but I would prefer to start Jasmine on the cmd line.
First I thought about adding the angular library to the "helpers" entry in jasmine.json. But it didn't work. The documentation of this file is unfortunately very poor. In the Angular documentation and tutorials it is always mentioned to use Karma. But my understanding is that Karma is only useful for testing with browsers, since it spawns an own webserver. This does not make sense in my case.
Would be great if somebody could give me a hint, thanks!

How do I rid my protractor tests of this error: Error while waiting for Protractor to sync with the page: {}

I have an Angular application that integrates with AudioEye, Twitter, and some other third party applications. We use lazy loading to load our components so our app has no rootElement app. Our login page is a different Angular application. When I run my test suite I keep seeing this error:
Error while waiting for Protractor to sync with the page: {}
Each time the tests run I get different results due to this error, and I am wondering I can resolve the issue. Any ideas or theories would be greatly appreciated. Thanks in advance.
STACK:
Cucumber 9.5
Protractor: 2.5.1
node: 0.12
I think these cases are still solved by turning the Protractor-to-Angular synchronization off. Put the following into onPrepare() in your config, or into a beforeEach/beforeAll of your test:
browser.ignoreSynchronization = true;
Note that, unfortunately, this would eliminate one of the advantages of using Protractor for testing AngularJS applications. Now, the tests may require additional explicit waits via browser.wait().
Follow-up links:
Using Protractor Test with Bootstrapped AngularJS
Protractor and angular manual bootstrap
In your config file add- rootElement : 'html'. and when you navigate to non-angualar-page use browser.ignoreSynchronization=true and use protractor commands.

AngularJS unit test directly in browser with Jasmine but without Karma or nodejs or any test runners

We can test normal JavaScript code in the browser using only Jasmine (by only I mean without Karma or any other test runners or nodejs).
Can we do this for AngularJS projects as well? If yes, how?
Sure. It's exactly the same like with plain js. You just need to use the methods for angular testing.
Take a look at this post:
http://dennis-nerush.blogspot.co.il/2015/08/creating-masked-input-directive-with.html

How can I run common code in tests using Karma + Jasmine + AngularJS?

I'm using Karma to run my unit tests against an AngularJS application. The problem is that I use the ui-router plugin, and the fact that it makes some XHR requests to run the templates forces me to mock those requests. Therefore, I see myself repeating this for every test file:
beforeEach(function($templateCache) {
$templateCache.put('templates/layout.html', '');
$templateCache.put('templates/dashboard/index.html', '');
$templateCache.put('templates/session/login.html', '');
});
How can I run this piece of code for all my unit tests? I tried googling, but no luck. Also, should I be doing this in some other way? Please share your opinions.
Thank you all.
You should look into ng-html2js-preprocessor:
https://github.com/karma-runner/karma-ng-html2js-preprocessor
It will batch up all your templates into a template cache module (that uses $templateCache under the hood) that you can use:
describe('SOMETHING', function() {
beforeEach(module('templates'));

Cordova + Angular: How to test?

I've just recently started fooling around with Cordova for a mobile App. For now the code base is quite small. I've also used AngularJS to drive my javascript. Now that I have reached a stable state, I would like to investigate ways to unit test the code I just wrote. Thing is, I'm not finding any useful resource for the pair. Angular suggests either Karma (unit) or Protractor (scenarios), but I'm finding quite hard to bootstrap them both with a Cordova App, since this is not supposed to run inside the browser, but within some kind of container where cordova can be loaded. Are there already some good test-driven approaches in the open source market regarding test driven development of hybrid apps?
I think that correct approach would be to have cordova.mocks.js included in tests that will mock out cordova dependencies. And then unittest as usual.
I think there is no way at the moment to test the parts of cordova that would call functionality from plugins.
But you could use Karma or Protractor as you would in the browser (eventually with some mocks for cordova and cordova plugins), which require some additional if conditionals to run the app without a physical device
Ie if (window.cordova && cordova.plugins.thePluginExample) { /* Code that uses plugins [...] */ }
You can use "phonegap server" even if you're using cordova, also you can run on the device with cordova run <platform> --device.
You can track issues on the CLI output of both methods.
To help others who get here with the same question as I did...
You probably don't need to bootstrap with Cordova. Use mocks as stand-ins.
Since Cordova attaches to window, you can write your app code to inject $window and mock cordova with with standard mocking.
Example with mocha / chai:
/**
* Test case for AngularJS module that does something when platform = 'ios'
*/
describe('platform = "ios"', function() {
var $window;
beforeEach('inject', inject(function(_$window_) {
$window = _$window_;
$window.cordova = {
platformId: 'ios',
}
}));
it('verifies cordova mock platform = "ios"', function() {
expect($window.cordova.platformId).to.equal('ios');
});
it('does something', function() {
// ...
});
});

Resources