終於來到Angular Taiwan線上讀書會,第6季最後一集了
想起來當時我也不是每集看直播
事後看youtub影片,也是看20~30分鐘就睡著
(個人有學習容易睡著的現象)
先打一下預防針:
由於第3季的講者大都有準備文件(有的我有附網址、有的我找不到)
為了避免抄襲,還是請大家去看講者提供的文章
所以第3季的心得會爛爛的
(目前第3季快寫完了,比起第6季有點爛尾現象)
重點是如果有看到我寫超精采的,一定值得初學者去多看幾次影片!!
小弟在最後一天(9/17)開賽後,庫存只有10篇文章左右
(目前庫存只有5篇左右)
最近為了趕文章,所以文章品質低下,希望後面如果趕到安全時,可以回來
畢竟一集30~70分鐘左右,講者只是分享重點,說明這是值得學的好東西
再來還是要自己去看官網文件、其他人寫的文件、或開源的專案
接下來明天就會看第3季的各種測試啦
以前也是超想學,但又是各種睡著,一直學不起來
利用鐵人賽強迫自己看過一輪
最後再來看第5季RxJS
跟第6季不同的事,
第3季、第5季有一些如果比較難打成筆記的,可能會2~3集合併成一篇
一些要付費或我完全試不出來的,也可能會跳過
最後一集Testing由Leo老師擔任終結者
影片網址:
https://www.youtube.com/watch?v=peaaGkcoTtY&list=PL9LUW6O9WZqgUMHwDsKQf3prtqVvjGZ6S&index=1
行為是否符合預期class或一個module
來看一下ng new出來就有的app.component.spec.ts
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
  beforeEach(async(() => {
  ^^^^^^^^^ 在每一個it之前,都會先跑一次。跑完再跑it
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
  
  // afterEach,在每一個it之後都執行1次
  // 抽出每個it重複的部分
  // beforeAll,在所有的it之前,執行1次
  // afterAll,在所有的it之後,執行1次
  
  // xdiscribe,乎略整個describe
  // xit ,乎略這個it
  
  // discribe跟it都可以巢狀
  // beforEach的影響範圍就是跟discribe同一層
  
  it('should create the app', () => {
      ^^^^^^^^^^^^^ 說明這個測試案例要幹麻的
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    // 有無成功拿到class的實體
    expect(app).toBeTruthy();
  });
  it(`should have as title 'day03'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    // app.title是否等於'day03'
    expect(app.title).toEqual('day03');
  });
  it('should render title in a h1 tag', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    // h1的內容是否包含'Welcome to day03!'
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to day03!');
  });
});
ng test 或 npm run test(定義在package.json)
來自己寫個簡單的測試,測function
app.component.spec.ts
// 1、先從使用的角度寫測試案例
// class有哪些功能?怎麼使用這些功能?
// 有一個hi的function,當傳入xxx時,會回傳Hi, xxx
it('預期看到 Hi, Leo',() => {
^^ 測試案例
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    except(app.hi('Leo')).toEqual('Hi, Leo');
                                   ^^^^^^^ 若相等就綠燈
app.component.ts
// 2、寫好測試案例,再來寫hi()
hi(name: string): string{
    return `Hi, ${name}`;
            ^^^^^^^^^^^
}
運作關係是不是正確的使用者的角度來檢示是否符合使用者需求期望的結果
重構 -> 紅燈 -> … )發生測試失敗時,失敗的原因點是否有被測試案例含蓋到?
TDD 測試驅動開發(Test-Driven Development)
ATTD 驗收測試驅動開發(Acceptance Test Driven Developent)
TDD
單元測試 -> 整合測試 -> 驗收測試
        <-        <-         ATTD
ATTD實務上的困難點
需求還沒定下來開發前提供驗收測試案例(Test Case)
系統再造,或開發自己的產品(如:需求與寫程式的同一人)。較好實現美好的ATTDbeforEach的影響範圍就是跟discribe同一層
const count = 0;
describe('第1層的describe', () => {
    beforEach(() => { count+=1; });
    it('1-1',()=>{...});
    
    describe('第2層的describe', () => {
        // 1-1、1-2不會觸發第2層的describe 的 beforEach
        beforEach(() => { count+=1; }); 
        it('2-1',()=>{...});
    }
    
    it('1-2',()=>{...});
}
export const mockData = {
    success: false,
    errorCode: 0, // 所有的errorCode測一試
    response: {
        data: 'Leo' // 餵各種前端吃的data是否仍能正常,如:極端值
    }
}
app.component.ts
假設有一個取資料的function
getData(): any {
    return {
        success: false,
        errorCode: 9999, 
        response: {
            data: null 
        }
    }
}
取代app實例裡的getData(),並改為回傳mockData
const data = spyOn(app, 'getData').and.returnValue(mockData);