2013-06-03 65 views
4

我使用jasmine来测试我的应用程序,现在我的代码中没有任何按钮
但我想写一个测试,在该测试中我可以检查点击事件是否被触发或不。
你可以简单地认为我想点击没有按钮的事件。使用茉莉花测试套件测试点击事件

这是我事先没有

scenario('checking that click event is triggered or not', function() { 

    given('Sigin form is filled', function() { 

    }); 
    when('signin button is clicked ', function() { 
     spyOn($, "click"); 
     $.click(); 

    }); 
    then('Should click event is fired or not" ', function() { 
     expect($.click).toHaveBeenCalled(); 
    }); 
}); 

感谢。

+0

我不认为..this需要jQuery的标签....现在取出...... – bipen

+0

哪一个@bipen? – Ancient

+0

我的意思是说这个问题被标记为jquery ...我认为你的问题是无关的..所以我删除了jquery标签.... – bipen

回答

5

我通常倾向于做的是create a stub并将事件分配给存根。然后触发click事件,并检查是否有人称之为

describe('view interactions', function() { 
    beforeEach(function() { 
     this.clickEventStub = sinon.stub(this, 'clickEvent'); 
    }); 

    afterEach(function() { 
     this.clickEvent.restore(); 
    }); 

    describe('when item is clicked', function() { 
     it('event is fired', function() { 
      this.elem.trigger('click'); 
      expect(this.clickEventStub).toHaveBeenCalled(); 
     }); 
    }); 
});