神刀安全网

#3 Testing with Jasmine – matchers and other features

Welcome again to my “testing with Jasmine” series. Previously, we discussed how Jasmine uses a behaviour-driven development approach. I announced then that this time I will write something about matchers, test cleanup and preparation, and about disabling and pausing tests.

TL;DR; matchers are kinds of assertions; test preparation can be done using beforeEach / beforeAll methods and test teardown might be done using afterEach / afterAll methods; test suite can be disabled by changing the method describe to xdescribe ; a single test can be paused by changing the it method to xit or by removing the function callback or by invoking the pending method inside the spec callback

Matchers

In the previous post I already mentioned matchers. They are just functions which are called as a chain after the expect function. We pass the value to be tested as a parameter of the expect function and, by calling a matcher, we can make an assertion. For example:

expect(car.type).toBe('coupe');

Additionally, we can deny the matcher by injecting not between the expect method call and the matcher:

expect(car.type).not.toBe('minivan');

Of course, toBe is not the only matcher available in Jasmine. Please see below a full list:

  • toBe – actually it’s just a === comparison
  • toEqual – allows you to compare objects
  • toMatch – comparison by the use of a regular expression
  • toBeDefined – checks if the value is defined
  • toBeUndefined – checks if the expression is equal to the undefined value
  • toBeNull – allows you to check if the value is null
  • toBeTruthy , toBeFalsy – used to test boolean casting, for example: var foo = 'foo'; expect(foo).toBeTruthy();
  • toContain – checks if the array contains a value
  • toBeLessThan , toBeGreaterThan – tests if the value is less/greater than the given one
  • toBeCloseTo – this is nice… allows you to check if the expression is close to the given value, for example: expect(3.14).toBeCloseTo(3);
  • toThrow – checks if the expression throws an exception
  • toThrowError – tests if a specific exception was thrown

I think all of these are really helpful. But if they are not enough for you, you can use a special fail function to construct you own assertions. Here’s how you can to it:

describe("a car", () => {   var assert = function(test, callBack) {     if (test) {       callBack();     }   };    it("should have 4 wheels", () => {     const car = new Car();        assert(car.wheelCount > 4, () => {       fail("the car has more wheels!!");     });   }); });

As you can see we have an assert function which calls a callback only if the test variable is true. We can then use it to make an assertion – if wheelCount property of the car object is greater than four, the callback is invoked and then the fail function is called. The fail function can take an error message as a parameter – it will be shown in the test results in case of failure. Instead of the message you can also pass on an Error object.

Other features: setup and cleanup

All test frameworks allow us to prepare before and clean after the execution. This is usually done by suitable functions. This is very similar in Jasmine. To do something before every single spec in the test suite we have to use the beforeEach function; to do something after every single spec in the test suite we have to use the afterEach function. Please take a look at the example below:

describe("A spec (with setup and tear-down)", () => {   var foo;     beforeEach(() => {     foo = 0;     foo += 1;   });     afterEach(() => {     foo = 0;   });     it("is just a function, so it can contain any code", () => {     expect(foo).toEqual(1);   });     it("can have more than one expectation", () => {     expect(foo).toEqual(1);     expect(true).toEqual(true);   }); });

As you can see, in the second spec we still expect the foo variable to be equal to 1 – this is because we assign a 0 to it after every spec execution.

We can also do something only once before all specs in the test suite and only once after all specs in the test suite. To do so, we can use the beforeAll and afterAll functions:

describe("A spec with only preparation and teardown only once", () => {   var foo;    beforeAll(() => {     foo = 1;   });    afterAll(() => {     foo = 0;   });    it("sets the initial value of foo before specs run", () => {     expect(foo).toEqual(1);     foo += 1;   });    it("does not reset foo between specs", () => {     expect(foo).toEqual(2);   }); });

This time, the foo variable was initialised with a 1 value and changed in the first spec. The second spec is proof that the initialisation and cleanup was done only once per test suite.

Other features: disabling and pause

The last thing in today’s post. We can temporarily disable a test suite or pause the execution of particular specs. To disable a suite we have to add an x prefix to the describe function:

xdescribe("A spec", () => {   it("is just a function, so it can contain any code", () => {     expect(true).toBe(true);   }); });

Of course we should always remember that if the tested code is changed we should change the test to cover the new implementation – we should not just disable it and forget about it 😉

Besides disabling test suites, we can also make a single spec pending. This will result in suspension of the test execution but it will still be visible in the test results with a pending status. Actually we have three ways to pause a spec:

  • we can add an x prefix to the it function:
xit("pending can be declared by 'xit'", () => {   expect(true).toBe(false); });
  • we can just remove a callback function:
it("pending can be declared with 'it' but without a function");
  • and finally, we can call a pending function inside the callback:
it("pending can be declared by calling 'pending' in the spec", () => {   expect(true).toBe(false);   pending('this is why it is pending'); });

Easy right?

Summary

Ok, that’s all for today. But I will continue this journey through the Jasmine testing capabilities in further posts. Next time we will talk about “spies” which are kinds of“stubs” in the Jasmine framework.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » #3 Testing with Jasmine – matchers and other features

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮