简体   繁体   中英

How to check if a method has been called on a given jQuery selector?

I'm using jasmine to conduct testing.

Basically I want to make sure that a specific method is being executed for a given jQuery selector.

eg

$('.hidden-tag').show()

Currently I have the following:

describe("Test", function () {
  beforeEach(function () {
    spyOn(window, '$').andCallThrough();

    loadFixtures('my_fixture.html');
  });

  it("should call show method on the jQuery selector '.hidden-tag'", function () {
    expect($).toHaveBeenCalledWith('.hidden-tag');
  });

});

The first part works. How do I get it to check that the 'show' method is being called?

Use :visible selector like,

alert($('.hidden-tag').is(":visible"));

Use $('.hidden-tag').is(":visible") in if condition like

if($('.hidden-tag').is(":visible"))
{
    expect($).toHaveBeenCalledWith('.hidden-tag');
}

Try install Fire Bug in Firefox. In that open Console and you will get if there is any error in calling 'show' method.

Also You can insert alert(message) just before and after the method. If previous alert() is not called, then problem lies somewhere before 'show' and if after alert() is not called, then 'show' got some bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM