📜 ⬆️ ⬇️

Jasmine - additional features

This article is a continuation of the first part of “Introduction to Jasmine” . Here we consider the additional features of the Jasmine test framework, namely:




')
For convenience, we will consider testing in the browser, and for conciseness, examples are given using CoffeeScript ( examples in JavaScript ).

In Jasmine function call tracking and call parameters is done using spyOn . The spyOn function is passed two parameters — the object for which the function is being called, and the name of the function to be monitored:

spyOn(window, 'isNaN') 


In the usual use of spyOn, the original function is not called.

Examples are given using the small Person class:

 class Person name: null age: 0 constructor: (@name, @age) -> getName: -> @name setName: (value) -> @name = value getAge: -> @age addYear: -> @age += 1 


When testing using spyOn, you can track the number of calls, their parameters and each call separately:

 describe "Spy", -> person = null beforeEach -> person = new Person("Jim", 25) it "  ", -> spyOn(person, 'getName') person.getName() expect(person.getName).toHaveBeenCalled() it "  ", -> spyOn(person, 'addYear') person.addYear() person.addYear() expect(person.addYear.calls.length).toEqual(2) it " ", -> spyOn(person, 'setName') person.setName("Ira") expect(person.setName).toHaveBeenCalledWith("Ira") #     it "    ", -> spyOn(person, 'setName') person.setName("Ira") expect(person.setName.mostRecentCall.args[0]).toEqual("Ira") it "    ", -> spyOn(person, 'setName') person.setName("Ira") expect(person.setName.calls[0].args[0]).toEqual("Ira") 


When using spyOn with andCallThrough , the original function will be called:

  it "  ", -> spyOn(person, 'getName').andCallThrough() expect(person.getName()).toEqual("Jim") expect(person.getName).toHaveBeenCalled() 


If it is necessary to return a certain value from a function, then for this you need to call spyOn along with andReturn

  it "  ", -> spyOn(person, 'getName').andReturn("Dan") expect(person.getName()).toEqual("Dan") expect(person.getName).toHaveBeenCalled() 


When using spyOn with andCallFake , instead of calling the original function, the specified function will be called:

  it "  ", -> spyOn(person, 'getAge').andCallFake(-> return 5 * 11) expect(person.getAge()).toEqual(55) expect(person.getAge).toHaveBeenCalled() 


To create a function without an implementation, you can use createSpy , with all the features available for testing the usual spy. The only parameter that takes createSpy is the name of the function to identify.

  it "  ", -> concat = jasmine.createSpy('CONCAT') concat("one", "two") expect(concat.identity).toEqual('CONCAT') #     expect(concat).toHaveBeenCalledWith("one", "two") expect(concat.calls.length).toEqual(1) 


Creating a stub object is done with createSpyObj . As parameters, createSpyObj takes the name of an object and an array of strings, which is a list of stub object methods:

  it "  ", -> button = jasmine.createSpyObj('BUTTON', ['click', 'setTitle', 'getTitle']) button.click() button.setTitle("Help") expect(button.click).toBeDefined() expect(button.click).toHaveBeenCalled() expect(button.setTitle).toHaveBeenCalledWith("Help") expect(button.getTitle).not.toHaveBeenCalled() 


Object type checking is performed by calling jasmine.any , to which the expected type is transmitted:

  it " ", -> spyOn(person, 'setName') person.setName("Ira") expect(person.setName).toHaveBeenCalledWith(jasmine.any(String)) 


Synchronous testing of setTimeout / setInterval calls is performed using jasmine.Clock.useMock . To move the time forward, use the jasmine.Clock.tick call, which passes the time in milliseconds:

 describe "", -> callback = null beforeEach -> callback = jasmine.createSpy('TIMER') jasmine.Clock.useMock() it " timeout ", -> setTimeout((-> callback()), 100) #    100ms expect(callback).not.toHaveBeenCalled() jasmine.Clock.tick(101) #    101ms expect(callback).toHaveBeenCalled() 


To run tests in Jasmine, as a rule, use a small script:

 #   jasmineEnv = jasmine.getEnv() jasmineEnv.updateInterval = 250 currentWindowOnload = window.onload window.onload = -> currentWindowOnload() if currentWindowOnload execJasmine() execJasmine = -> jasmineEnv.execute() #   htmlReporter = new jasmine.HtmlReporter() jasmineEnv.addReporter(htmlReporter) jasmineEnv.specFilter = (spec) -> htmlReporter.specFilter(spec) 


Jasmine supports several types of test reports, the main ones are:



If you have questions or comments, I will be glad to answer them.

References:
github.com/pivotal/jasmine - project page on GitHub
www.inexfinance.com/en/blog/2013/2/17/jasmine_additional_features - English version of this article.
github.com/inex-finance/blog-examples/tree/master/jasmine - code examples from this article

Source: https://habr.com/ru/post/169699/


All Articles