这是一种最简单的绑定类型,knockout JS定义了如下的绑定属性:
knockout 提供了一系列对整个页面中一小段代码进行在何种条件下是否存在进行控制的绑定,它们是:
非常容易,因为knockout js 公开了一系列用于此绑定的属性:
不常用
)不常用
)有:
这里有三种可能的情况:
自定义的绑定被定义成 ko.bindingHandlers对象的新的属性,包含两个功能:
两个功能均有相同的参数列:
bindingContext.$data
Jasmine 提供了详细的匹配列表:
Spy 初始化的选项,包括:
用法:
spyOn(container, "myFunc").and.callThrough();
用法:
spyOn(container, "myFunc").and.returnValue(50);
用法:
spyOn(container, "myFunc").and.callFake(function (arg) {
return arg + 10;
});
用法:
spyOn(container, "myFunc").and.throwError("this is an exception");
用法:
container.myFunc.and.stub();
expect(container.myFunc.calls.any()).toEqual(true);
expect(container.myFunc.calls.count()).toEqual(2);
expect(container.myFunc.calls.argsFor(0)).toEqual([10]);
expect(container.myFunc.calls.allArgs()).toEqual([
[10],
[20]
]);
expect(container.myFunc.calls.all()).toEqual([
{
object: container,
args: [10]
},
{
object: container,
args: [20]
}
]);
expect(container.myFunc.calls.mostRecent()).toEqual({
object: container,
args: [20]
});
expect(container.myFunc.calls.first()).toEqual({
object: container,
args: [10]
});
container.myFunc.calls.reset();
全局性的spyOn 一个实际对象的方法,Jasmin也提供两个方法去创建spy, 而不需要任何已存在的对象
//create a spy
var mySpy = jasmine.createSpy("mySpy");
//call the spy
mySpy(23, "an argument");
//verify expectations as a regular spy
expect(mySpy).toHaveBeenCalled();
expect(mySpy).toHaveBeenCalledWith(23, "an argument");
//create a mock with spies
var myMock = jasmine.createSpyObj("myMock", ["aSpy",
"anotherSpy"]);
//call the spies
myMock.aSpy();
myMock.anotherSpy(10);
//verify expectations as regular spies
expect(myMock.aSpy).toHaveBeenCalled();
expect(myMock.aSpy.calls.argsFor(0).length).toEqual(0);
expect(myMock.anotherSpy).toHaveBeenCalled();
expect(myMock.anotherSpy).toHaveBeenCalledWith(10);
异步代码的测试模式总结,设想一个这样一个经典的服务:
var MyService = function () {
this.fetchResult = function (callback) {
jQuery.ajax("url", {
success: function (result) {
callback(result);
}
});
};
};
fetchResult 将一个callback方法作为参数,只有当ajax调用成功后才会将其作为参数被传入
describe("Given an async service", function () {
var myService, myResult;
beforeEach(function (done) {
myService = new MyService();
spyOn(myService, "fetchResult").and.callFake(function
(callback) {
setTimeout(function () {
callback(10);
done();
}, 50);
});
myService.fetchResult(function (result) {
myResult = result;
});
});
it("when service is tested with the async pattern, then it can be
simulated", function (done) {
expect(myResult).toEqual(10);
done();
});
});