2016-04-24 50 views
0

當我使用颱風和故事板時,我正在努力模擬視圖控制器的依賴。當我嘗試修補依賴項時,我想模擬該修補程序似乎沒有任何影響。如何在使用Typhoon時從測試中模擬故事板視圖控制器依賴關係?

請任何人都可以幫忙嗎?

這裏是我的颱風組件:

#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 

是否可以嘲笑利用颱風時加載故事板視圖控制器依賴呢?

回答

0

於是,經過多一點研究,我已經找到了解決辦法,但必須使用PList Integration切換到AppDelegate Integration實施initialFactory方法:

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集成。

相關問題