How to spyOn a value property (rather than a method) with Jasmine

前端 未结 10 2228
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 04:12

Jasmine\'s spyOn is good to change a method\'s behavior, but is there any way to change a value property (rather than a method) for an object? the code could be

10条回答
  •  暖寄归人
    2020-12-01 04:39

    I'm using a kendo grid and therefore can't change the implementation to a getter method but I want to test around this (mocking the grid) and not test the grid itself. I was using a spy object but this doesn't support property mocking so I do this:

        this.$scope.ticketsGrid = { 
            showColumn: jasmine.createSpy('showColumn'),
            hideColumn: jasmine.createSpy('hideColumn'),
            select: jasmine.createSpy('select'),
            dataItem: jasmine.createSpy('dataItem'),
            _data: []
        } 
    

    It's a bit long winded but it works a treat

提交回复
热议问题