Kiwi 单元测试在 Xcode 中从未失败

Kiwi unit tests never failing in Xcode

出于某种原因,我的测试每次都通过了。即使我添加

fail(@"failed");

Xcode 还是说 "test succeeded"

有什么想法吗?

这是我的规格

#import "SDRViewController.h"
#import <UIKit/UIKit.h>
#import <Kiwi/Kiwi.h>

SPEC_BEGIN(SDRViewControllerSpec)

describe(@"SDRViewController", ^{
    __block SDRViewController *viewController = [[SDRViewController alloc]init];

    beforeAll(^{
        [viewController setupCollectionView];
    });
    describe(@"viewController.collectionView", ^{

        describe(@"collectionViewLayout", ^{
            UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)viewController.collectionView.collectionViewLayout;
            [[flowLayout shouldNot]beNil];
            [[theValue(flowLayout.sectionInset) should]equal: theValue(UIEdgeInsetsZero)];
            fail(@"failed");
             });

        });
});

SPEC_END

您的代码不会失败,因为它不包含单元测试。因此,失败为零,Xcode 认为是成功。您应该将要测试的代码包装在 it 块中:

describe(@"collectionViewLayout", ^{
    it(@"has a valid flow layout", ^{
        UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)viewController.collectionView.collectionViewLayout;
        [[flowLayout shouldNot]beNil];
        [[theValue(flowLayout.sectionInset) should]equal: theValue(UIEdgeInsetsZero)];
        fail(@"failed");
    });
});

如果您想了解有关 Kiwi 积木的更多信息,有一本好书:https://itunes.apple.com/us/book/test-driving-ios-development/id502345143?ls=1&mt=11