Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the `it()` function here doing?

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!'); }); 
like image 421
George Avatar asked Feb 05 '15 20:02

George


People also ask

What is the IT function in JavaScript?

In JavaScript, we have many frameworks dealing with individual concerns, and Jasmine is one of those many. It is a unit testing framework, and it has its documentation already packed up. One of the functions of this framework is the it() function that enables the user/admin to get the total outcome of the code lines.

How do you call a function?

You call the function by typing its name and putting a value in parentheses. This value is sent to the function's parameter. e.g. We call the function firstFunction(“string as it's shown.”);

What is closure in JavaScript stack overflow?

A closure is a link between a function and its outer lexical (ie. as-written) environment, such that the identifiers (variables, parameters, function declarations etc) defined within that environment are visible from within the function, regardless of when or from where the function is invoked.

What is describe in angular?

describe(string, function) functions take a title and a function containing one or more specs and are also known as a suite or test suite. it(string, function) functions take a title and a function containing one or more expectations and are also known as specs. expect(actual) functions take a value, called an actual.


2 Answers

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/

like image 199
Aaron Greenwald Avatar answered Sep 24 '22 19:09

Aaron Greenwald


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

like image 31
vlio20 Avatar answered Sep 24 '22 19:09

vlio20