簡體   English   中英

獼猴桃:有沒有辦法通過運行時變量定義動態數量的示例?

[英]Kiwi: Is there a way to have a dynamic number of example, defined by a runtime variable?

我正在尋找一種測試嵌套對象的屬性的方法。 本質上,我有一個規范可以驗證我們從外部服務獲得的結果。 由於我不想為要測試的每個示例都對服務進行大量的調用,因此在規范開始時就發出了一次請求,然后我們對驗證響應進行了一系列期望。

用代碼示例最好地解釋一下(顯然,它不能按預期工作):

    beforeAll(^{
        wait = [self waitFor:^(id handler) { [session.account all:handler]; }];
        accounts = wait.result;
    });

    it(@"should make a successfull request", ^{
        [[wait should] beSuccessful];
    });

    it(@"returns a non-nil list of accounts", ^{
        [[accounts shouldNot] beNil];
    });

    it(@"should have at least 1 account", ^{
        [[[accounts.accounts should] haveAtLeast:1] account];
    }); 

    context(@"each account", ^{
        for (Account* account in accounts.accounts) {
            it(@"should have a name", ^{
                [[account.name shouldNot] beEmpty];
            });
            it(@"should have an accountID", ^{
                [[account.accountID shouldNot] equal:@"bla"];
            });
        }
    });
});

這是我感興趣的“每個帳戶”上下文。基本上,該調用返回一組帳戶,我希望每個帳戶都有效。

我可以解決這個問題,並在it()塊中進行循環,這樣可以工作,但它不能告訴我哪個項目有問題,並且所有驗證程序都在有問題的驗證程序之后運行(例如,在nil account.name遇到) )返回Trying to add another verifier without specifying a matcher for the previous one. 錯誤。 所以,真的沒有用。

我考慮過僅測試返回的json,因為不管我們從服務中得到什么響應,我們都有一套規范(單元)測試我們自己的組件。 但這是行不通的,因為我們實際上並不關心確切的數據集(也容易更改),只是格式應該正確。

當使用本地測試數據編寫規范時,我會為層次結構中的每個級別編寫多個規范,但我希望將它們結合在一起:我想測試從服務器收到的一個響應的整體。

我嘗試通過在運行測試時添加更多KWExamples來動態地執行此操作,但這似乎不起作用。

有任何想法嗎(或者我是否將其誤用於本不打算做的事情)?

(也發布為Github問題: https : //github.com/allending/Kiwi/issues/435

每個規格都必須是唯一的名稱。 在您的示例中,您要添加x具有相同名稱的規格。 我認為最好只有2個規格。 只需迭代兩個數組:

context(@"each account", ^{
    it(@"should have a name", ^{
       for (Account* account in accounts.accounts) {
            [[account.name shouldNot] beEmpty];
       }
    });
    it(@"should have an accountID", ^{
       for (Account* account in accounts.accounts) {
            [[account.accountID shouldNot] equal:@"bla"];
       }
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM