Dojo測試之基本用法

【翻譯】https://github.com/dojo/framework/blob/master/docs/en/testing/basic-usage.md

目前創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、拉薩網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

Dojo 的 @dojo/cli-test-intern 提供了一個(gè)健壯的測試框架。它能高效地測試小部件的輸出并確認(rèn)是否如你所愿。

功能描述
極簡 API 用于測試和斷言 Dojo 部件預(yù)期的虛擬 DOM 和行為的精簡 API。
單元測試 單元測試是指運(yùn)行在 node 和瀏覽器上的測試,用于測試獨(dú)立的代碼塊。
功能測試 功能測試通過 Selenium 運(yùn)行在瀏覽器中,模擬用戶與軟件的交互來測試整體功能。
斷言模板 斷言模板能構(gòu)建期望的渲染函數(shù),以驗(yàn)證部件的輸出。

測試 Dojo 應(yīng)用程序

Dojo 使用 @dojo/cli-test-intern 運(yùn)行 tests 文件夾下的單元測試和功能測試。

你可在 node 中快速運(yùn)行測試。

命令行

dojo test

運(yùn)行測試

Dojo 支持兩種類型的測試方法:單元測試和功能測試。單元測試是運(yùn)行在 node 和本地 Selenium 通道上的測試,用于測試獨(dú)立的代碼塊。功能測試通過 Selenium 運(yùn)行在瀏覽器中,模擬用戶與軟件的交互來測試整體功能。在 Selenium 上運(yùn)行單元測試和功能測試時(shí),必須先使用 @dojo/cli-build-app 進(jìn)行適當(dāng)?shù)臉?gòu)建。

以下命令僅執(zhí)行單元測試。

命令行

dojo test --unit --config local

以下命令使用 Selenium 在本地的 headless Chrome 實(shí)例中運(yùn)行功能測試。

命令行

dojo test --functional --config local

單元測試

Dojo 自帶用于測試部件的 harness API。

src/tests/unit/widgets/Home.ts

const { describe, it } = intern.getInterface('bdd');
import harness from '@dojo/framework/testing/harness';
import assertionTemplate from '@dojo/framework/testing/assertionTemplate';
import { w, v } from '@dojo/framework/widget-core/d';

import Home from '../../../src/widgets/Home';
import * as css from '../../../src/widgets/styles/Home.m.css';

const baseTemplate = assertionTemplate(() => v('h2', { classes: [css.root] }, ['Home Page']));

describe('Home', () => {
    it('default renders correctly', () => {
        const h = harness(() => w(Home, {}));
        h.expect(baseTemplate);
    });
});

harness API 能讓你核實(shí)渲染部件的輸出是否如你所愿。

  • 它是否按預(yù)期渲染?
  • 事件處理器是否按預(yù)期工作?

功能測試

功能測試允許你加載一個(gè)頁面并在瀏覽器中執(zhí)行你的代碼,以更好的測試部件的行為。

當(dāng)編寫測試用例時(shí),你可以控制頁面中測試的交互行為,來單擊按鈕并驗(yàn)證頁面的內(nèi)容。

tests/functional/main.ts

describe('routing', () => {
    it('profile page correctly loads', ({ remote }) => {
        return (
            remote
                // 在本地的 node 服務(wù)器中加載 HTML 文件
                .get('../../output/dev/index.html')
                // 根據(jù) id 找到超鏈接標(biāo)簽的
                .findById('profile')
                // 單擊鏈接
                .click()
                // 結(jié)束此操作
                .end()
                // 找到 h2 標(biāo)簽
                .findByTagName('h2')
                // 獲取 h2 標(biāo)簽中的文本
                .getVisibleText()
                .then((text) => {
                    // 核實(shí) profile 頁面中 h2 標(biāo)簽中的內(nèi)容
                    assert.equal(text, 'Welcome Dojo User!');
                })
        );
    });
});

斷言模板

斷言模板提供了一種創(chuàng)建基本斷言的方法,該方法允許你在每個(gè)測試中修改期望輸出中的部分內(nèi)容。

一個(gè)部件可根據(jù)屬性值渲染不同的內(nèi)容。

src/widgets/Profile.ts

export interface ProfileProperties {
    username?: string;
}

export default class Profile extends WidgetBase<ProfileProperties> {
    protected render() {
        const { username } = this.properties;
        return v('h2', { classes: [css.root] }, [`Welcome ${username || 'Stranger'}!`]);
    }
}

你可以使用 @dojo/framework/testing/assertionTemplate 創(chuàng)建一個(gè)斷言模板。

tests/unit/widgets/Profile.ts

// 創(chuàng)建一個(gè)斷言
const profileAssertion = assertionTemplate(() =>
    v('h2', { classes: [css.root], '~key': 'welcome' }, ['Welcome Stranger!'])
);

describe('Profile', () => {
    it('default renders correctly', () => {
        const h = harness(() => w(Profile, {}));
        // 基于基本斷言測試
        h.expect(profileAssertion);
    });
});

使用在斷言模板中定義的 ~key 屬性,你可以為任何要測試的虛擬 DOM 提供一個(gè)值。在 .tsx 中對應(yīng)的是 assertion-key 屬性。

tests/unit/widgets/Profile.ts

describe('Profile', () => {
    it('default renders correctly', () => {
        const h = harness(() => w(Profile, {}));
        h.expect(profileAssertion);
    });

    it('renders given username correctly', () => {
        // 使用給定的用戶名更新期望的結(jié)果
        const namedAssertion = profileAssertion.setChildren('~welcome', () => ['Welcome Kel Varnsen!']);
        const h = harness(() => w(Profile, { username: 'Kel Varnsen' }));
        h.expect(namedAssertion);
    });
});

使用斷言模板的 setChildren 方法,傳入指定的 ~key 來定位一個(gè)虛擬 DOM,并修改該虛擬 DOM 的結(jié)構(gòu),然后返回更新的斷言模板,你可以基于新的斷言模板測試部件的輸出。

當(dāng)前標(biāo)題:Dojo測試之基本用法
地址分享:http://bm7419.com/article6/jjsgog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版靜態(tài)網(wǎng)站、ChatGPT品牌網(wǎng)站制作、建站公司、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司