19

I use this yeoman generator: https://github.com/Swiip/generator-gulp-angular

It installs three testing applications: Jasmine, Karma , Protractor According to this article (Should I be using Protractor or Karma for my end-to-end testing?), I should use: Karma for small tests of e.g. a single controller. Protactor if I want to test the whole app and simulate an user browsing through my app. According to this blog (http://andyshora.com/unit-testing-best-practices-angularjs.html) I would use Jasmine for unit testing and Karma for end-to-end integration tests.

I guess Jasmine is the language where tests are written and the other two execute the code, is that correct? Also if I never wrote a test which is more important to learn first/ to focus on?

Community
  • 1
  • 1
Andi Giga
  • 3,744
  • 9
  • 38
  • 68
  • See my answer here for more detailed discussion of use cases, advantages and limitations of Karma and Protractor: http://stackoverflow.com/questions/17070522/can-protractor-and-karma-be-used-together/29619467#29619467 – Dmitri Zaitsev Apr 14 '15 at 16:50

1 Answers1

46

Karma is a test-runner, so it runs your test. Jasmine is the framework that let you write test

In my opinion in Angularjs you :

  • must unit-test services, because your business code is there.
  • should unit-test controller, because users actions are there.
  • may unit-test custom directives (if you plan to share that directive with others, it's a must)

Protractor is made for E2E testing (tests navigation like a real user). It combines WebDriverJS with Jasmine and lets you write End-to-End tests (you simulate a real browser and taking real actions) with Jasmine syntax.

That kind of test is also really important in a web app.

You should not test everything, especially at the start of the project, those kinds of tests usually come with a high level of maintenance (i.e., when you change a screen you may have to change the test).

What I do is test the critical path and features. I made a reading app, so in my case, it was login, sign up, payment, access book, and access reader.

MaxRocket
  • 920
  • 1
  • 12
  • 26
Boris Charpentier
  • 3,515
  • 25
  • 28
  • Ok but what can I do with Protractor than? Is it also a test-runner and do I write there also in jasmine? – Andi Giga Jan 30 '15 at 10:55
  • Ah ok cool, how do test payment, because providers have transaction fees. Do have a cheap hidden product to save fees etc.? So I guess I start with learning Jasmine (like the blog article I posted) and the test suits are probably anyways controlled by gulp. Do have a good resource for learning Jasmine otherwise I just google for it? – Andi Giga Jan 30 '15 at 11:17
  • For payment I only check the access to the screen and the redirect, not the actuel payment. Jasmine has some doc http://jasmine.github.io/2.0/introduction.html but it's really more made to go and pick what you need, when you need it. What I use most is, spyOn, expect, toBe, toEqual, toHaveBeenCall, and specific to angularjs $httpBackend for network call stub. – Boris Charpentier Jan 30 '15 at 11:23
  • While I'm here, when you'r in test(ie having loading angularMock), you should know that you have to manually resolve promise calling $scope.$digest(); and $httpBackend.flush() for network call, the principle are that the angular team don't like asynchronous test, and so the behaviour in the test made that syncronous. And I think it's great :) – Boris Charpentier Jan 30 '15 at 11:25