初學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是://數值比較時定義精度,先四捨五入後再比較
但我確實沒看懂這個實例。
以致於我想去找一下規律。。。然後發現我的調試都出了問題。。在報錯的情況下,我還是沒怎麼看懂這個樣例。