Spying on JQuery Selectors in Jasmine

前端 未结 6 1441
执念已碎
执念已碎 2020-12-02 15:29

I am unit testing some JavaScript with Jasmine and wish to spy on (mock) an element of the DOM that is accessed by a jQuery selector.

My spec is:

it(         


        
6条回答
  •  失恋的感觉
    2020-12-02 16:00

    Seems like I found good solution

        it "should open past statuses", ->
          # We can't use $('.past') here cause each time $('.past') called it returns different objects
          # so we need to store spy in variable
          showSpy = spyOn($.fn, 'show')
          # do the stuff
          $('.show-past').click()
          # then check if 'show' action was called
          expect($.fn.show).toHaveBeenCalled()
          # and if it realy our object
          expect(showSpy.mostRecentCall.object.selector).toEqual('.past')
    

    This is not based on your code but i hope this can help someone. And, yes, example in CoffeScript.

提交回复
热议问题