MyTimeFinder
MyTimeFinder

Reputation: 1807

basic testacular/jasmine/angular setup and usage

I am new to Jasmine, but would like to do more TDD in Javascript. I've been using the Angular library from Google, and I know that Testacular was specifically created for testing Angular apps.

I have read through both the Jasmine documentation and watched the Testacular setup video, but I can't get the most basic testing to work. Assume I have three files:

modules.js
controller.js
appSpec.js

modules.js has my module definition with a few .factory(...) services and a few .directive(...) custom directives. controller.js houses my controllers for wiring up the modules to the html view.

So far, so good. Next I have added appSpec.js. Let's say I want to use it to test a service in my module called, "Data" that has a method, "getData()" which returns a resource.

In testactular init I have told it to watch all three files. I run Testacular and it tells me it is watching the correct files. Super.

What I don't understand is how I get the Jasmine spec know how to look at the module and controller so that they can be tested. If I simply say:

describe('Data Service', function(){
  it ('should retrieve two items from the database', function(){
    data = Data.getData() //my angular service
    expect(data.length).toBe(2);
  });
});

Not surprisingly, it has no idea what Data.getData() is.

It seems obvious that somehow I am supposed to bring my module definition and controllers into the spec before I begin writing suites. It must be so obvious that I don't see in the documentation how people are doing it. Tutorials just seem to start writing specs in the spec.js file and assume all is well.

I have seen other posts here where similar questions are being asked, but admittedly they all have a foundation I seem to be lacking. For example, one post talks about not manually creating an instance of the controller, but rather inject dependencies. Why is he creating a new $rootScope object, how is his module being referenced, etc...

I understand that my question is probably just a lack of basic understanding of the Jasmine framework, but I can't seem to squeeze any more understanding from the Jasmine readme file. Can someone point me to a basic explanation of how this is supposed to work?

Thanks.

Upvotes: 4

Views: 3333

Answers (2)

Progdom
Progdom

Reputation: 91

The way that you include your definitions for your tests is through the karma.config.js file

files = [ JASMINE, JASMINE_ADAPTER, '../app/lib/icg/object.js', '../app/lib/icg/geometry.js', '../app/lib/icg/ubiquity.js', '../test/unit/icgUbiquitySpec.js', '../test/unit/icgObjectSpec.js' ];

Here you define which files get loaded into your browser, the first files that don't include the word 'Spec' are the definitions and the ones that do include are my test files. You need to include your definitions BEFORE your tests so that they are defined before you run the tests, which is why I include all my spec files last.

Upvotes: 0

Andrew Joslin
Andrew Joslin

Reputation: 43023

Try doing module('myModule') in the jasmine test.

Here are some open source angular projects that have great tests to look at:

angular-app

angular-ui

bootstrap

Upvotes: 2

Related Questions