Is there a generator for jasmine angular controller tests?
Asked Answered
D

5

19

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.

Dilapidation answered 6/8, 2015 at 13:55 Comment(2)
Check this. Hope this will help you.Inclusive
I've started writing one a month ago, because didn't find anything too. It's in active development now, but core concepts are already present. I'll give an update in this post, when I'll have a working version published in npm, but I can't give you exact estimates.Carminecarmita
C
10

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.

Carminecarmita answered 18/9, 2015 at 8:11 Comment(2)
this it AWESOME! Where can I +1 you or something?Loafer
Really awesome.Spurrier
P
6

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');
    });
  }
});
Peroxidase answered 13/8, 2015 at 8:31 Comment(0)
M
5

I am using yeoman with generator-angular to generate our scripts & tests.

yo angular:directive myDirective yo angular:service myService yo angular:controller myController etc..

will generate both the script and spec templates. I am using Karma and Jasmine.

You could also always write your own yeoman generator.

Marijane answered 17/8, 2015 at 18:13 Comment(2)
I should have been clearer in my original question. We have an existing angular application with no tests. I'd like a way to generate jasmine tests against existing controllers/services/directives.Dilapidation
it would be great to generate the test for the existing directive or controller, because the beforeEach blocks are always pretty much the same with all this tedious mocking and setting up jasmine spies...Loafer
L
0

I found this thing and it does a lot of good gob:

https://www.npmjs.com/package/generator-yosapy

Loafer answered 22/7, 2016 at 19:54 Comment(0)
Q
0

Leveraging the ai capabilities to generate unit test can streamline and fasten the unit testing process.

This library reads you component file and generates specific test cases for it.

npm i ai-test-gen-angular
export OPENAI_API_KEY=somekey
node ./node_modules/ai-test-gen-angular/index.js "relative/path/to/service/or/somecomponent.component.ts" "relative/path/to/tsconfig.json" 

This code will generate a unit test file with the written tests for the component or service that you mention in the command. Please note that you will need to have a openai api key to use this.

Original Answer here

Quadrate answered 26/10, 2023 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.