如何在使用Typhoon时模拟测试中的故事板视图控制器依赖项?

时间:2016-04-24 22:50:42

标签: ios objective-c automated-tests typhoon

在使用Typhoon和storyboard时,我很难模仿视图控制器依赖。当我尝试修补依赖项时,我想模拟补丁似乎没有任何影响。

请有人帮忙吗?

这是我的台风大会:

#import "ANYApplicationAssembly.h"
#import "ANYDatabase.h"
#import "ANYTableViewController.h"

@implementation ANYApplicationAssembly

- (ANYTableViewController *)tableViewController {
    return [TyphoonDefinition withClass:[ANYTableViewController class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(database) with:[self theDatabase]];
    }];
}

- (ANYDatabase *)theDatabase {
    return [TyphoonDefinition withClass:[ANYDatabase class]];
}

@end

而且,这是测试:

#import <OCMock/OCMock.h>
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "ANYTableViewController.h"
#import "ANYApplicationAssembly.h"
#import "ANYDatabase.h"

@interface ANYTableViewControllerTests : XCTestCase

@end

@implementation ANYTableViewControllerTests

ANYTableViewController* controller;
ANYDatabase* mockDatabase;

- (void)setUp {
    [super setUp];

    mockDatabase = OCMClassMock([ANYDatabase class]);

    ANYApplicationAssembly* assembly = [[ANYApplicationAssembly assembly] activate];
    TyphoonPatcher* patcher = [[TyphoonPatcher alloc] init];
    [patcher patchDefinitionWithSelector:@selector(theDatabase) withObject:^id{
        return mockDatabase;
    }];
    [assembly attachDefinitionPostProcessor:patcher];
    [assembly makeDefault];

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    controller = [storyboard instantiateViewControllerWithIdentifier:@"TableController"];
    [controller loadViewIfNeeded]; // force IBOutlets etc to be initialized
    XCTAssertNotNil(controller.view);
}

- (void)testShowsAllTheThings {
    // Given
    NSArray* allTheThings = @[@"all", @"the", @"things"];
    OCMStub([mockDatabase things]).andReturn(allTheThings);

    // When
    NSInteger sections = [controller numberOfSectionsInTableView:controller.tableView];
    NSInteger rows = [controller tableView:controller.tableView numberOfRowsInSection:0];

    // Then
    XCTAssertEqual(sections, 1);
    XCTAssertEqual(rows, 2);
}

@end

使用Typhoon时,是否可以模拟由故事板加载的视图控制器的依赖项?

1 个答案:

答案 0 :(得分:0)

所以,经过一番调查后,我找到了一个解决方案,但不得不从使用PList Integration切换到实现initialFactory方法的AppDelegate Integration

AppDelegate.h:

- (id)initialFactory;

AppDelegate.m:

- (id)initialFactory {
    TyphoonComponentFactory *factory = ([[TyphoonBlockComponentFactory alloc] initWithAssembly:[ANYApplicationAssembly assembly]]);
    [factory makeDefault];
    return factory;
}

ANYTableViewControllerTests.m:

- (void)setUp {
    [super setUp];

    mockDatabase = OCMClassMock([ANYDatabase class]);

    ANYApplicationAssembly* assembly = (ANYApplicationAssembly*) [TyphoonComponentFactory defaultFactory];
    TyphoonPatcher* patcher = [[TyphoonPatcher alloc] init];
    [patcher patchDefinitionWithSelector:@selector(theDatabase) withObject:^id{
        return mockDatabase;
    }];
    [assembly attachDefinitionPostProcessor:patcher];

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    controller = [storyboard instantiateViewControllerWithIdentifier:@"TableController"];
    [controller loadViewIfNeeded]; // force IBOutlets etc to be initialized
    XCTAssertNotNil(controller.view);
}

我有兴趣知道是否有任何方法可以使用PList集成来实现这一点/为什么它不能使用PList集成。