Bir yöntem üzerinde casusluk örneğim, "Beklenen casus handle_click çağrıldı." geçmesi gerektiğinde. Ancak, "Foo handle_click!" Adlı konsol günlüğüne ulaşıyorum, bu yüzden çağrıldığını biliyorum.Jasmine'in spyOn toHaveBeen ile ilgili sorunlar
Foo.js
function Foo() {
this.$btn = $('<a href="#">Foo</a>');
this.$btn.on('click', this.handle_click);
};
Foo.prototype.handle_click = function(evt) {
evt.preventDefault();
console.log('Foo handle_click called!');
};
Foo_spec.js: Ben yasemin-1.2.0, kullanıyorum
it('should be called when trigger is clicked', function() {
var foo = new Foo();
spyOn(foo, 'handle_click').andCallThrough();
foo.$btn.click();
expect(foo.handle_click).toHaveBeenCalled();
});
jasmin-html ve yasemin-jquery değil yasemin-sinon; En azından orada olduğunu sanmıyorum. Herhangi bir yardım çok takdir edilir!
Güncelleştirme Bu, aşağıdaki yanıt verildi.
Foo.js:
function Foo() { ... }
Foo.prototype.handle_click = function(evt) { ... }
$.fn.foo = function(options) {
return new Foo(this, options || {});
};
$.fn.foo.prototype = Foo.prototype;
Foo_spec.js: Ancak, ben bir jQuery Plugin durumunda çözüm belgelemek istedik
it('should be called when clicked', function() {
spyOn($.fn.foo.prototype, 'handle_click');
var plugin = $('#selector-for-plugin').foo();
plugin.$btn.click();
expect(plugin.handle_click).toHaveBeenCalled();
});
Nice! Öneriniz için prototipimi jQuery Plugin'e ekledim, böylece casusun başlatılmadan önce atanması sağlandı: '$ .fn.foo = function (options) {return new Foo (this, options || {}); }; ' ' $ .fn.foo = Foo.prototype; ' Teşekkürler! –