Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a generator for jasmine angular controller tests?

I'm looking for something that generates a boilerplate jasmine test for an angular controller. It seems you could pull the dependencies for the controller out and drop them into the spec and save some typing. I would be shocked if I were the first person to have this idea but I'm unable to find anything that does this, save a yeomen project that doesn't appear to work.

like image 689
Slartibartfast Avatar asked Aug 06 '15 13:08

Slartibartfast


People also ask

What is Jasmine testing framework and how do you use it for angular unit testing?

Jasmine is a behavior development testing framework. Unit tests are written using Jasmine and are run to see if individual parts of an application are working correctly. As a result, unit tests will either pass or fail depending on if the code is working correctly or has a bug.

What is the name of test runner in angular CLI * A pencil B Jasmine C Karma D none of these?

When creating Angular projects using the Angular CLI it defaults to creating and running unit tests using Jasmine and Karma. Whenever we create files using the CLI , as well as creating the main code file it also creates a simple Jasmine spec file named the same as the main code file but ending in . spec.

What is the role of Jasmine and karma in angular testing?

Jasmine is a behavior-driven development framework for testing JavaScript code that plays very well with Karma. Similar to Karma, it's also the recommended testing framework within the Angular documentation as it's setup for you with the Angular CLI. Jasmine is also dependency free and doesn't require a DOM.

What is the best way to test AngularJS components?

Angular provides a good boilerplate for writing testings with both Jasmine framework and Karma test runner. Using the Test Wrapper pattern in your tests is a good way to fully test the life cycle of a component and easily test how changes in a parent will effect the component you are testing.

What is Jasmine testing framework?

Jasmine is the framework we are going to use to create our tests. It has a bunch of functionalities to allow us the write different kinds of tests. karma. Karma is a task runner for our tests. It uses a configuration file in order to set the startup file, the reporters, the testing framework, the browser among other things.

How to run angular tests in karma?

An environment to run angular tests is being created using all the imports at the beginning of the file. TestBed is a powerful unit testing tool provided by angular, and it is initialized in this file. Finally, karma loads all the test files of the application matching their names against a regular expression.

What is automated testing in angular?

Automated testing has become a great way protect applications from new bugs. Let’s look at a pattern that can be used to test components. You have at least a boilerplate Angular application (one generated using ng new <app-name> will work), set up with Jasmine and Karma.


2 Answers

I've recently published my version of Angular JS unit test generator on npm - tleaf. Basically it tries to parse you source file looking for AngularJS units (controllers, services, etc) to extract information about unit name, module name and unit's dependencies. This information is used to create a unit test file, based on a template for this unit type. There is a default set of templates which have a pretty simple structure, it should be ok for general use. But it is also possible to create and use your own templates to generate unit test files. This is a very first version and I'll be happy to have any feedback.

like image 83
Michael Radionov Avatar answered Sep 25 '22 03:09

Michael Radionov


I don't know of a generator for tests but I have two ideas.

Some editors provide templates for "repeated" code. Like Live Templates for Webstorm. There are multiple projects on github providing jasmine templates for it.

You could also check ng-describe. It removes the boilerplate and makes testing simpler. Here's an example form their github:

ngDescribe({
  modules: 'A',
  inject: ['$rootScope', 'foo'],
  tests: function (deps) {
    it('finally a test', function () {
      deps.$rootScope.$apply();
      expect(deps.foo).toEqual('bar');
    });
  }
});
like image 43
m.brand Avatar answered Sep 22 '22 03:09

m.brand