簡體   English   中英

使用Typhoon時如何從測試中模擬情節提要視圖控制器的依賴性?

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

使用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

使用Typhoon時,是否可以模擬情節提要加載的視圖控制器的依賴關系?

因此,經過更多調查,我找到了一個解決方案,但不得不從使用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集成中不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM