1

我正在努力设置一个简单的网络 POST 请求存根。我已经尽可能多地从 OHHTTPStubs 文档和其他在线资源中建模,但我认为我一定遗漏了一些东西。我想查看基于该onStubActivation方法的日志记录调用的存根。我的测试看起来像:

#import "Cedar.h"
#import "OHHTTPStubs.h"
#import "Client.h"

SPEC_BEGIN(Spec)

describe(@"Client", ^{
    __block Client *subject;
    __block __weak id<OHHTTPStubsDescriptor> stub;

    beforeEach(^{
        subject = [[Client alloc] init];
        stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
            return YES;
        } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
            return [OHHTTPStubsResponse 
                        responseWithJSONObject:@{} 
                        statusCode:200 
                        headers:@{ @"Access-Token": @"new-token"}];
        }];
        stub.name = @"success-stub";

        [OHHTTPStubs onStubActivation:
            ^(NSURLRequest *request, id<OHHTTPStubsDescriptor> stub) {
                NSLog(@"%@ stubbed by %@.", request.URL, stub.name);
        }];
    });

    describe(@"-signInWithUsername:Password:SuccessBlock:FailureBlock:", ^{
       subjectAction(^{
            [subject signInWithUsername:@"email@domain.com"
                               Password:@"password"
                           SuccessBlock:^void(){NSLog(@"GREAT-SUCCESS");}
                           FailureBlock:^void(){NSLog(@"GREAT-FAILURE");}];
            });
        context(@"when the user/password is valid", ^{
            it(@"should update the auth token", ^{
                subject.token should equal(@"new-token");
            });
        });

    });
});

SPEC_END

客户端看起来像:

#import "Client.h"
#import "AFNetworking.h"

@interface Client ()

@property (nonatomic) NSString *token;
@property (nonatomic) AFHTTPRequestOperationManager *manager;

@end

@implementation Client

- (instancetype)init
{
    self = [super init];
    self.manager = [[AFHTTPRequestOperationManager alloc] init]];
    return self;
}

- (void)signInWithUsername:(NSString *)username
                  Password:(NSString *)password
              SuccessBlock:(void (^)())successBlock
              FailureBlock:(void (^)())failureBlock;
{
    [self.manager POST:@"http://localhost:3000/auth/sign_in" 
            parameters:nil 
               success:^(AFHTTPRequestOperation *operation, id responseObject) {
                   NSLog(@"JSON: %@", responseObject);
                   successBlock();
    }         
               failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                   NSLog(@"Error: %@", error);
                   failureBlock();
    }];
}

@end
4

0 回答 0