简体   繁体   English

使用Typhoon时如何从测试中模拟情节提要视图控制器的依赖性?

[英]How to mock storyboard view controller dependencies from tests when using Typhoon?

I'm struggling to mock a view controller dependency when using Typhoon and storyboards. 使用Typhoon和情节提要板时,我正在努力模拟视图控制器的依赖性。 When I try to patch the dependency I want to mock the patch doesn't seem to have any affect. 当我尝试修补依赖项时,我想模拟该修补程序似乎没有任何影响。

Please can anyone help? 任何人都可以帮忙吗?

Here's my Typhoon assembly: 这是我的台风大会:

#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

And, here's the test: 并且,这是测试:

#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

Is it possible to mock dependencies for view controllers loaded by storyboards when using Typhoon? 使用Typhoon时,是否可以模拟情节提要加载的视图控制器的依赖关系?

So, after a bit more investigating, I've found a solution but had to switch from using PList Integration to AppDelegate Integration implementing the initialFactory method: 因此,经过更多调查,我找到了一个解决方案,但不得不从使用PList Integration切换到实现initialFactory方法的AppDelegate Integration

AppDelegate.h: AppDelegate.h:

- (id)initialFactory;

AppDelegate.m: AppDelegate.m:

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

ANYTableViewControllerTests.m: 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);
}

I'd be interested to know if there's any way to get this to work using PList integration though / why it doesn't work using PList integration. 我想知道是否有任何方法可以使它在PList集成中起作用,但是/为什么在PList集成中不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Typhoon为Integration测试注入假,存根或模拟依赖项 - How to inject fake, stubbed or mock dependencies for Integration tests using Typhoon 台风从情节提要向视图控制器中注入属性 - Typhoon injecting property into view controller from storyboard 如何从情节提要创建模拟视图控制器 - How to create a mock view controller from storyboard Typhoon Storyboard:将IBOutlet视图注入Controller依赖项 - Typhoon Storyboard: Inject an IBOutlet View to a Controller dependency 使用情节提要板时如何使用Typhoon导航到导航控制器中的控制器 - How to navigate to a controller in a navigation controller using Typhoon when using storyboards 台风:情节提要中创建视图控制器的线循环依赖项 - Typhoon : wire circular dependency in storyboard created view controller 带有视图控制器属性的台风 - Typhoon with view controller property 如何使用storyBoard设置MFSlideviewcontroller的视图控制器? - How to set View Controller of MFSlideviewcontroller using storyBoard? 在页面视图控制器中以编程方式使用故事板中的视图控制器 - Using View Controller from storyboard programmatically in a Page View Controller 如何从View Controller中的情节提要中选择UIImage - How to select UIImage in storyboard from View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM