angular unit test-jasmine syntax-1

1. Suites (describe suite): describe the test content

Code example:

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

Sample code to explain:

describe () is used to group related specification content. Usually, each test file has a describe method at the top level.

Method parameters:

  Parameter 1: String, this string is used to name the current test module (suite).

  Parameter 2: Method, test code can be written inside the method, and one or more it () can be executed.

it () (spec specification) is a global function of jasmine. Like describe () function, it receives two parameters,

parameter:

  Parameter 1: String, used to name the title of the current spec

  Parameter 2: The method, which can contain one or more expectations of the test code status.

expect is an assertion that is either true or false.

  expect () receives an actual value that needs to be verified, and then connects a matching function (.toBe () in this example). The matching function receives the expected value to be compared.

  If the actual value matches the expected value successfully, ecpect executes the test successfully, and the matching fails, that is, the test execution fails.

Code example:

describe("A suit", function(){
  it("can have a negative case", function() {
    expect(false).not.toBe(true);
  });
});

Sample code to explain:

  Expect any matcher that is asserted before. You can use .not to compare the opposite of the expected value in the match function.

 

2. The describe () and it () functions, the code scope applies to the JavaScript code scope rules.

Code example:

describe("A suite is just a function", function() {
  var a;

  it("and so is a spec", function() {
    a = true;

    expect(a).toBe(true);
  });
});

Sample code to explain:

The describe () and it () function blocks can be used to write any executable code required for testing (scope applies to js code scope rules).

Therefore, the variables defined in describe can be used in it, and executed in accordance with the code flow, in turn.

 

3. To help the test suite eliminate duplicate settings and destroy unnecessary variables, Jasmine provides four functions: beforeEach (), afterEach (), beforeAll (), and afterAll ().

Code example:

describe("A suite with some shared setup", function() {
    var foo = 0;
    beforeEach(function() {
        foo += 1;
    });
    afterEach(function() {
        foo = 0;
    });
    beforeAll(function() {
        foo = 1;
    });
    afterAll(function() {
        foo = 0;
    });
});

Sample code to explain:

beforeEach: Executed before each it () in the suite that called it.

afterEach: Executed once after each it () in the suite that called it.

beforeAll: Executed before all it () in the suite calling it.

afterAll: Executed once after all it () in the suite calling it.

4. The method of sharing variables between beforeEach, it, and afterEach

Method 1: By defining a variable in describe, this variable can be shared between beforeEach, it, and afterEach

Method 2: Through this keyword, before describe, beforeEach, it, and afterEach share this object, that is, set this.a = 0 in beforeEach, and get this.a value in it.

But in it, variables are not shared.

Code example:

describe("A spec", function() {
  beforeEach(function() {
    this.foo = 0;
  });

  it("can use the `this` to share state", function() {
    expect(this.foo).toEqual(0);
    this.bar = "test pollution?";
  });

  it("prevents test pollution by having an empty `this` created for the next spec", function() {
    expect(this.foo).toEqual(0);
    expect(this.bar).toBe(undefined);
  });
});

 

5. The fail () function

Code example:

describe("A spec using the fail function", function() {
  var foo = function(x, callBack) {
    if (x) {
      callBack();
    }
  };

  it("should not call the callBack", function() {
    foo(false, function() {
      fail("Callback has been called");
    });
  });
});

Sample code to explain:

The fail function will cause the spec to fail,

Method parameters: failure message or error object.

 

6, describe () nesting

Code example:

describe("A spec", function() {
  var foo;

  beforeEach(function() {
    foo = 0;
    foo += 1;
  });

  afterEach(function() {
    foo = 0;
  });

  it("is just a function, so it can contain any code", function() {
    expect(foo).toEqual(1);
  });

  it("can have more than one expectation", function() {
    expect(foo).toEqual(1);
    expect(true).toEqual(true);
  });

  describe("nested inside a second describe", function() {
    var bar;

    beforeEach(function() {
      bar = 1;
    });

    it("can reference both scopes as needed", function() {
      expect(foo).toEqual(bar);
    });
  });
});

Sample code to explain:

describe () can be nested, and describe () can be defined at any level, which allows describe to be grouped into a function tree.

Before executing an it () (spec specification), jasmine executes each beforeEach function in order.

After executing it () (spec specification), jasmine executes the afterEach function in a similar manner.

 

7.Disabling suites

Code example:

xdescribe("A spec", function() {
  var foo;

  beforeEach(function() {
    foo = 0;
    foo += 1;
  });

  it("is just a function, so it can contain any code", function() {
    expect(foo).toEqual(1);
  });
});

 Sample code to explain:

xdescribe function, set to disable the suite.

Disabling kits for any of it (spec specifications) will be skipped at runtime, so their results will appear to hang.

8. Suspend it (spec specification)

Code example:

describe("Pending specs",function() {
    xit("can be declared 'xit'", function() {
        expect(true).toBe(false);
    });
    it ( "it (spec specification) without function body declaration, will also be marked as pending in the result." );
    it("can be declared by calling 'pending' in the spec body", function() {
        expect(true).toBe(false);
        pending('this is why it is pending');
    });
});

 

Sample code to explain:

Suspended it (spec specification), that is, xit will not be executed, but their names will appear in the result in the suspended state.

It (spec specification) without function body declaration will also be marked as pending in the result.

If pending is called anywhere in the it (spec specification), no matter what assert asserts, the it (spec specification) will be marked as pending, and the string passed in pending () will be considered as the reason display

 

Guess you like

Origin www.cnblogs.com/jing5990/p/12759571.html