spyOn(window, 'isNaN')
class Person name: null age: 0 constructor: (@name, @age) -> getName: -> @name setName: (value) -> @name = value getAge: -> @age addYear: -> @age += 1
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")
it " ", -> spyOn(person, 'getName').andCallThrough() expect(person.getName()).toEqual("Jim") expect(person.getName).toHaveBeenCalled()
it " ", -> spyOn(person, 'getName').andReturn("Dan") expect(person.getName()).toEqual("Dan") expect(person.getName).toHaveBeenCalled()
it " ", -> spyOn(person, 'getAge').andCallFake(-> return 5 * 11) expect(person.getAge()).toEqual(55) expect(person.getAge).toHaveBeenCalled()
it " ", -> concat = jasmine.createSpy('CONCAT') concat("one", "two") expect(concat.identity).toEqual('CONCAT') # expect(concat).toHaveBeenCalledWith("one", "two") expect(concat.calls.length).toEqual(1)
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()
it " ", -> spyOn(person, 'setName') person.setName("Ira") expect(person.setName).toHaveBeenCalledWith(jasmine.any(String))
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()
# 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)
Source: https://habr.com/ru/post/169699/
All Articles