• Front-end development, the preparation of test cases is indispensable. Why write test cases?
  • Well, that’s a matter of opinion,
  • From my point of view, in large-scale project integration, code changes can be well restrained, and timely understanding of functon not work can avoid online problems.
  • Test cases are written mostly based on nodeJS development environment, of course test cases can also use the Web version.
  • This article is based on the introduction of personal computer development environment, if not enough, please Google yourself.

Replace the test cases in the Spec folder with the jasmine official address

The node installation

node install

 brew search nodejs 
 brew install nodejs
Copy the code

jasmine install

NPM install jasmine NPM install -g jasmine // Create spec folder and jasmine. Json -- global: jasmine init -- local: NPX jasmine initCopy the code

Jasmine

objective

  • All expect success in an IT, the whole IT success
  • One Expect fails, an entire IT test fails

The core concept

  • BeforeAll global variables are initialized beforeAll test cases are called

  • AfterAll resets global variables afterAll test case calls.

  • BeforeEach a specific variable is initialized beforeEach test case is executed.

  • AfterEach resets specific variables afterEach test case is executed.

  • Describe defines an IT set of test cases

  • It defines a single test case

  • Expect, back with Matcher, asserts that the test case execution was successful

  • SpyOn performs proxy proxy for Service and Component function, replaces corresponding business logic when IT runs, and carries out black box test to ensure functional integrity of code.

  • The sample

describe('Hello test'.function() {
 var hello;
 var status = 0;
 
 beforeAll(function(){
   hello = new Hello();
 });

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

 afterAll(function(){
   hello = undefined;
 });
 afterEach(function(){
    status = 0;
 });

 it("test spyon with no arguments".function() {
   spyOn(hello,'sayHello');
   hello.sayHello();
   //pass
   expect(hello.sayHello).toHaveBeenCalledWith();
 });

});
Copy the code

Other concepts

  • Fail marks the test case as failing, passing the cause of the failure
describe("A spec using the fail function".function() {
  var foo = function(x, callBack) {
    if(x > 60) { callBack(); }}; it("should not call the callBack".function() {
    foo(70, function() {
      fail("should the capacity will be under 60 ");
    });
  });
});

Copy the code

spyOn

The three necessary steps:

  • 1.编写spyOn(obj, methodName)
  • 2. Call the function
  • 3. To expect

model

function Hello(){}
Hello.prototype.sayHello = function(){
	console.log("say hello jasmine");
}
Hello.prototype.getName = function(){
	console.log("will return xiaoming");
	return 'xiaoming'
}
Hello.prototype.getSum = function(a,b){
	console.log("will return sum");
	return a+b;
}
Copy the code

1. Functions with no arguments

describe('Hello test'.function() {
	var hello;
	beforeEach(function() {
	hello = new Hello();
	});
	it("test spyon with no arguments".function() {
	spyOn(hello,'sayHello');
	hello.sayHello();
	//pass
	expect(hello.sayHello).toHaveBeenCalledWith();
	});
});
Copy the code

2. Functions with arguments

it('test spyon with argments'.function(){
   spyOn(hello, 'getSum');
   hello.getSum(5, 10);
   // also passes
   expect(hello.getSum).toHaveBeenCalledWith(5, 10);
});
Copy the code

3. Functions with return values

it("test spyon with no arguments but with return value".function() {
	spyOn(hello,'getName')
	hello.getName();
	//fail
	expect(value).toBe('xiaoming'); }); // Add the additional parameter it()"test spyon with no arguments but with return value".function() {
	spyOn(hello,'getName').and.returnValue('xiaoming');
	const value = hello.getName();
	//pass
	expect(value).toBe('xiaoming');
});
 
 //or
 
it("test spyon with no arguments but with return value".function() {
	// spyOn(hello,'getName').and.returnValue('xiaoming');
	spyOn(hello,'getName').and.callFake(function() {return 'xiaoming';
	});
	const value = hello.getName();
	//pass
	expect(value).toBe('xiaoming');
});
Copy the code

Function real call

1. No parameters are required

it("test spyon callthrough with no arguments".function() {
	spyOn(hello,'sayHello').and.callThrough(); hello.sayHello(); //pass, and the function triggers Console expect(hello.sayHello).tohaveBeencalled (); });Copy the code

2. Take parameters

it('test spyon callthrought with argments'.function(){
		spyOn(hello, 'getSum').and.callThrough(); const value = hello.getSum(5, 10); // Also passes, console expect(hello.getsum). ToHaveBeenCalledWith (5, 10); expect(value).toBe(15); });Copy the code

xdescribe

Skip all test cases in Describe and will not be executed

xdescribe('Hello test'.function() {
	var hello;
	beforeEach(function() {
		hello = new Hello();
	});

	it("test spyon with no arguments".function() {
		spyOn(hello,'sayHello');
		hello.sayHello();
		//pass
		expect(hello.sayHello).toHaveBeenCalledWith();
	});
});
Copy the code

Xit – Skips test cases and will not be executed

xit("test spyon callthrough with no arguments".function() {
spyOn(hello,'sayHello').and.callThrough();
hello.sayHello();
//pass
expect(hello.sayHello).toHaveBeenCalled();
});
Copy the code

fdescribe

Focus on the set of test cases, the rest will not be executed. Equivalent to xdescribe inverse.

fit

Focused test cases

fdescribe('test with callThrough'.function(){

  var hello;
  beforeEach(function() {
    hello = new Hello();
  });

  fit("test spyon callthrough with no arguments".function() {
    spyOn(hello,'sayHello').and.callThrough();
    hello.sayHello();
    //pass
    expect(hello.sayHello).toHaveBeenCalled();
  });
  
  it('test spyon callthrought with argments'.function(){
    spyOn(hello, 'getSum').and.callThrough();
    const value = hello.getSum(5, 10);
    // also passes
    expect(hello.getSum).toHaveBeenCalledWith(5, 10);
    expect(value).toBe(15);
  });

});
Copy the code

pending

Paused test case, ignoring test execution results.