iosobjective-cxcodeunit-testingkiwi

Kiwi unit tests never failing in Xcode


For some reason my tests are passing every time. Even when I add

fail(@"failed");

Xcode still says "test succeeded"

Any ideas?

Heres what my spec looks like

#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

Solution

  • Your code can't fail as it contains no unit tests. Thus, there are zero failures, which Xcode considers as success. You should wrap the code you want to test within an it block:

    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");
        });
    });
    

    If you want to learn more on the Kiwi blocks, there a good book on that: https://itunes.apple.com/us/book/test-driving-ios-development/id502345143?ls=1&mt=11.