初学jasmine,今天就学了这些,后期进行更新。咳咳,文章章法就这样吧,有机会再整理~。
jasmine就是一个前端的测试软件。
从Github上(https://github.com/pivotal/jasmine/releases)下载所需的Jasmine版本。
下载完成之后,直接打开SpecRunner.html即为Demo,除了引入Jasmine框架之外,只需引用自己所需测试的js文件以及Jasmine测试脚本引可。
然后我下载完了,就打开了这个demo,看看长成了啥样:
嗯,大概就是这样的页面,看着界面的大概功能就是,会报错!
嗯,因为不知道大概jasmine解压包里面什么有用,于是
把它打包和我的css,html文件放到了一块儿。
jasmine文件夹下面的东西长这样:
1.html长这样:
我自己跑了一下,想实现成它deme那样,然后成功了:
然后开始学习jasmine里面的语句:
describe("My first Jasmine test", function() {
it("a spec with an expectation", function() {
expect(1).toBe(1);
expect(1===1).toBe(true);
expect('a').not.toBe('b');
});
it("an other spec in current suite", function() {
expect(true).toBe(true);
});
});
describe("My first Jasmine test", function() {
it("nothing", function() {
});
});
/*describe()可以嵌套。也可以写同层级的。*/
describe('a case',function(){
describe('describe nested test',function(){})
it('with an expactation',function(){
expect(true).toBe(true);
})/*it是测试语句,其中所有语句真,则结果真。
it的语句结构。
it('函数名字',function(){expect(变量).toBe(……)})
expect后面期待的东西不同,看调用的toBe函数是什么,另作比较。
toBe() 变量
not.to……()
toEqual()
toMatch()
toBeUndefined()
toBeNull()
toBeTruthy() //bool值
toContain() //在数组里面找值,能找到那个值,就为真
toBeLessThan() //前者比后者(指toBeLessThan)小,返回真。
toBeCloseTo() //数值比较时定义精度,先四舍五入后再比较
toThrowError()
*/
})
/*describe 和 it 都是函数,所以它们可以包含任何js代码, 在 describe 中声明的变量和方法,能在 it中被访问。
it 代表的是具体的测试,当其中所有的断言都为true时,则该测试通过; 否则测试失败*/
describe('a case is just a function',function(){
var a=10;
it('and here is a test',function(){
var a=true;
expect(a).toBe(true);
})
})
it('"toMatch" matcher is for regular expression', function(){
var message = "foo bar baz";
expect(message).toMatch(/bar/);
expect(message).toMatch("bar");
expect(message).not.toMatch(/quue/);
});
/*
/bar/ "bar" 相当于查找这个字符串里面有没有bar这个单词,省略了。
*/
/*var这个值可以定义任何类型的变量,甚至可以变相等价于类。
var a=1;
var a="character string";
var a={
firstmember=1;
secondmember="string";
thirdmember=true;
}
*/
it('"toBeCloseTo" matcher is for precision match comparison', function(){
var n = 1.99, e = 2.35, a = 2.2, b = 3.1;
expect(e).not.toBeCloseTo(n, 2);
expect(e).toBeCloseTo(n, 0);
expect(a).toBeCloseTo(b, 3);
expect(a).toBeCloseTo(b, 1);
expect(a).toBeCloseTo(b, 2);
expect(a).toBeCloseTo(b, 0);
});
学习之中遇到的问题:
我学习jasmine里面的有一个函数我自己看了一会儿确实看得不是特别懂。
我查了查网上,说toBeCloseTo是://数值比较时定义精度,先四舍五入后再比较
但我确实没看懂这个实例。
以致于我想去找一下规律。。。然后发现我的调试都出了问题。。在报错的情况下,我还是没怎么看懂这个样例。