简体   繁体   English

目标C-单元测试和模拟对象?

[英]Objective C - Unit testing & Mocking object?

- (BOOL)coolMethod:(NSString*)str
{
     //do some stuff
     Webservice *ws = [[WebService alloc] init];
     NSString *result = [ws startSynchronous:url];
     if ([result isEqual:@"Something"])
     {
         //More calculation
         return YES;
     }
     return NO;
}

I am using OCUnit In the following method how can i mock my WebService Object, or the result to the method "startSynchronous" to be able to write an independent unit test? 我正在使用OCUnit在以下方法中,如何模拟我的WebService对象或方法“ startSynchronous”的结果以能够编写独立的单元测试?

Is it possible to inject some code in there to either create a mock web service or return a mock data on startSynchronous call? 是否可以在其中注入一些代码以创建模拟Web服务或在startSynchronous调用上返回模拟数据?

One way is to use categories and override methods you want, you can even override the init method to return a mock object: 一种方法是使用类别并覆盖所需的方法,甚至可以覆盖init方法以返回模拟对象:

@interface Webservice (Mock)
- (id)init;
@end

@implementation Webservice (Mock)
- (id)init
{
     //WebServiceMock is a subclass of WebService
     WebServiceMock *moc = [[WebServiceMock alloc] init];
     return (Webservice*)moc;
}
@end

The problem with this is that if you want to make the object return different results in different tests in 1 test file you cannot do that. 这样做的问题是,如果要使对象在1个测试文件中的不同测试中返回不同的结果,则不能这样做。 (You can override each method once per test page) (您可以在每个测试页上一次覆盖每种方法)

EDIT: 编辑:

This is an old question I posted, I thought I would update the answer to how I write testable code and unit test it nowadays :) 这是我发布的一个老问题,我认为我将更新如今如何编写可测试代码和对其进行单元测试的答案:)

ViewController Code ViewController代码

@implementation MyViewController
@synthesize webService;

- (void)viewDidLoad
{
   [super viewDidLoad];

   [self.webService sendSomeMessage:@"Some_Message"];
}

- (WebService *)webService
{
   if (!_webService)
      _webService = [[WebService alloc] init];

   return _webService;
}

@end

Test Code 测试代码

@implementation MyViewControllerTest

- (void)testCorrectMessageIsSentToServer
{
   MyViewController *vc = [[MyViewController alloc] init];
   vc.webService = [OCMock niceMockForClass:[WebService class]];

   [[(OCMockObject *)vc.webService expect] sendSomeMessage@"Some_Message"];
   [vc view]; /* triggers viewDidLoad */
   [[(OCMockObject *)vc.webService verify];
}

@end

Building on top of the WebService answer from aryaxt, here's a little trick to be able to get different results in different test. 基于aryaxt的WebService答案,这是一个小技巧,可以在不同的测试中获得不同的结果。

First, you need a singleton object which will be used to store the desired answer, right before the test TestConfiguration.h 首先,您需要一个单例对象,该对象将用于存储所需的答案,就在测试TestConfiguration.h之前

#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>


void MethodSwizzle(Class c, SEL orig, SEL new);

@interface TestConfiguration : NSObject


@property(nonatomic,strong) NSMutableDictionary *results;

+ (TestConfiguration *)sharedInstance;


-(void)setNextResult:(NSObject *)result
     forCallToObject:(NSObject *)object
              selector:(SEL)selector;


-(NSObject *)getResultForCallToObject:(NSObject *)object selector:(SEL)selector;
@end

TestConfiguration.m TestConfiguration.m

#import "TestConfiguration.h"


void MethodSwizzle(Class c, SEL orig, SEL new) {
    Method origMethod = class_getInstanceMethod(c, orig);
    Method newMethod = class_getInstanceMethod(c, new);
    if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
        class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    else
        method_exchangeImplementations(origMethod, newMethod);
};

@implementation TestConfiguration


- (id)init
{
    self = [super init];
    if (self) {
        self.results = [[NSMutableDictionary alloc] init];
    }
    return self;
}

+ (TestConfiguration *)sharedInstance
{
    static TestConfiguration *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[TestConfiguration alloc] init];
        // Do any other initialisation stuff here
    });
    return sharedInstance;
}


-(void)setNextResult:(NSObject *)result
     forCallToObject:(NSObject *)object
            selector:(SEL)selector
{
    NSString *className =  NSStringFromClass([object class]);
    NSString *selectorName = NSStringFromSelector(selector);

    [self.results setObject:result
                     forKey:[[className stringByAppendingString:@":"] stringByAppendingString:selectorName]];
}

-(NSObject *)getResultForCallToObject:(NSObject *)object selector:(SEL)selector
{
    NSString *className =  NSStringFromClass([object class]);
    NSString *selectorName = NSStringFromSelector(selector);

    return [self.results objectForKey:[[className stringByAppendingString:@":"] stringByAppendingString:selectorName]];

}



@end

Then you would define your "Mock" category to define mock methods , such as : 然后,您将定义“模拟”类别以定义模拟方法,例如:

#import "MyWebService+Mock.h"
#import "TestConfiguration.h"

@implementation MyWebService (Mock)


-(void)mockFetchEntityWithId:(NSNumber *)entityId
                           success:(void (^)(Entity *entity))success
                           failure:(void (^)(NSError *error))failure
{

    Entity *response = (Entity *)[[TestConfiguration sharedInstance] getResultForCallToObject:self selector:@selector(fetchEntityWithId:success:failure:)];

    if (response == nil)
    {
        failure([NSError errorWithDomain:@"entity not found" code:1 userInfo:nil]);
    }
    else{
        success(response);
    }
}

@end

And finally, in the tests themselves, you would swizzle the mock method in the setup , and define the expected answer in each test, before the call 最后,在测试本身中,您将调用setup中的模拟方法,并在调用之前定义每个测试中的预期答案。

MyServiceTest.m MyServiceTest.m

- (void)setUp
{
    [super setUp];

    //swizzle webservice method call to mock object call
    MethodSwizzle([MyWebService class], @selector(fetchEntityWithId:success:failure:), @selector(mockFetchEntityWithId:success:failure:));  
}

- (void)testWSMockedEntity
{
    /* mock an entity response from the server */
    [[TestConfiguration sharedInstance] setNextResult:[Entity entityWithId:1]
                                      forCallToObject:[MyWebService sharedInstance]
                                               selector:@selector(fetchEntityWithId:success:failure:)];

    // now perform the call. You should be able to call STAssert in the blocks directly, since the success/error block should now be called completely synchronously.
}

Remark : in my example, the TestConfiguration uses class/selector as a key instead of object/selector. 备注:在我的示例中,TestConfiguration使用类/选择器作为键而不是对象/选择器。 That means every object of the class will use the same answer for the selector. 这意味着该类的每个对象将对选择器使用相同的答案。 That is most likely your case, as webservice are often singleton. 这很可能是您的情况,因为Web服务通常是单例的。 But it should be improved to an object/selector maybe using the objet's memory address instead of its class 但是应该使用对象的内存地址而不是其类将其改进为对象/选择器

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM