35

The following snippet of code is from angular's documentation. What is the it() function here doing (I'm assuming it has some conventional meaning because otherwise no context seems to be given for its meaning)? I don't see any reference to it on angular's site. It's also hard to google due to it's name. The context is with regards to code testing.

it('should say hello', function() {
  var scopeMock = {};
  var cntl = new MyController(scopeMock);

  // Assert that username is pre-filled
  expect(scopeMock.username).toEqual('World');

  // Assert that we read new username and greet
  scopeMock.username = 'angular';
  scopeMock.sayHello();
  expect(scopeMock.greeting).toEqual('Hello angular!');
});
3

2 Answers 2

42

The it() function is defined by the jasmine testing framework, it is not part of angular per se. You'll see it in angular's documentation because they are encouraging you (for good reason) to get in the habit of writing tests for your code, and demonstrating how the code will work in a test.

The it() function defines a jasmine test. It is so named because its name makes reading tests almost like reading English. The second argument to the it() function is itself a function, that when executed will probably run some number of expect() functions. expect() functions are used to actually test the things you "expect" to be true.

Read more about jasmine testing on the jasmine framework's website: http://jasmine.github.io/

8

it is related to tests with jasmine framework, you can find more information here: http://jasmine.github.io/
https://docs.angularjs.org/guide/unit-testing

1
  • @George, did the answer helped you? if so please accept it :)
    – vlio20
    Feb 11, 2015 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.