如何在运行时更新TyphoonConfig

时间:2014-06-17 19:51:26

标签: typhoon

我在testcase中创建工厂

        + (TyphoonComponentFactory*)integrationTestFactory
{
    static TyphoonComponentFactory* factory;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[
            [MainAssembly assembly],
            [Kernel assembly],
            [NetworkComponents assembly],
            [PersistenceComponents assembly]
        ]];

        //Other config here. . . 
        TyphoonConfigPostProcessor *configurer = [TyphoonConfigPostProcessor configurer];
        [configurer useResourceWithName:@"testConfig.properties"];
        [factory attachPostProcessor:configurer];
        //this may be necessary
        [factory makeDefault 

    });
    return factory;
}

然后访问它

  - (void)setUp
{
    [super setUp];

    factory_instance = [LoginTest integrationTestFactory];
}

最后访问程序集会给出配置密钥的错误值

DAO *dataManager = [(DaoAssembly*)factory_instance DAO];

装配如下

- (ID)DAO {

return [TyphoonDefinition withClass:[DAO class] configuration:^(TyphoonDefinition* definition)
        {
            [definition useInitializer:@selector(shareManager)];
            [definition injectProperty:@selector(apikey) with:TyphoonConfig(@"api.key")];
            [definition injectProperty:@selector(dataParser) with:[self dataParser]];
            definition.scope = TyphoonScopeSingleton;

        }];

}

1 个答案:

答案 0 :(得分:0)

我无法重现您正在体验的内容,并通过以下测试用例进行了验证。

请亲自尝试这个简单的例子 - 或许它会解释问题所在。

<强> SomeProperties.properties:

damsels.rescued=12

<强> Knight.h / .M:

@interface Knight : NSObject

@property(nonatomic) NSUInteger damselsRescued;

@end

<强> MyAssembly.m:

- (id)knight
{
    return [TyphoonDefinition withClass:[Knight class] configuration:
        ^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(damselsRescued)
             with:TyphoonConfig(@"damsels.rescued")];        
    }];
}

<强> IntegrationTests.m:

@interface IntegrationTests : XCTestCase
{
    TyphoonComponentFactory *_factory;
}


@end

@implementation IntegrationTests

+ (TyphoonComponentFactory *)integrationTestFactory
{
    static TyphoonComponentFactory *factory;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[
            [MyAssembly assembly],
        ]];

        //Other config here. . .
        TyphoonConfigPostProcessor *configurer = [TyphoonConfigPostProcessor configurer];
        [configurer useResourceWithName:@"SomeProperties.properties"];
        [configurer useResourceWithName:@"SomeOtherProperties.properties"];
        [factory attachPostProcessor:configurer];
        [factory makeDefault];
    });
    return factory;
}


- (void)setUp
{
    _factory = [IntegrationTests integrationTestFactory];
}

- (void)testPropertiesAreSet
{
    Knight *knight = [(InfrastructureComponentsAssembly *) _factory knight];
    XCTAssertEqual(knight.damselsRescued, 12);
}

@end