1

アプリのネットワーク コードを単体テストしようとしていますが、テストに失敗してすべてが機能していることを確認したいという問題がいくつかあります。事は、それは常に通過しているということです。問題になる可能性がある場合に備えて、Xcode6-Beta7を使用しています

テストには Specta を使用し、OHHTTPStubs を使用してネットワーク要求をスタブ化し、応答でステータス コードを返します。

これが私のセットアップとティアダウンのコードです

beforeEach(^{
    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return YES;
    } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
        return [OHHTTPStubsResponse responseWithData:nil statusCode:200 headers:nil];
    }].name = @"My Test Stub";

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

afterAll(^{
    [OHHTTPStubs removeAllStubs];
});

テストの次の部分 (問題のある領域) では、NSMutableURLRequest を処理するクラスのインスタンスを作成し、それが nil でないかどうかを確認します。

context(@"Given that I create a http request with a URL", ^{
    __block NLXApiBaseServices *baseService = [[NLXApiBaseServices alloc] init];
    NSMutableURLRequest *testRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];

    it(@"should have a base service created", ^{
        expect(baseService).toNot.beNil();
    });

この最初のテストが機能し、次のテストでスタブが使用されます。私のスタブは 200 を返すはずなので、これは失敗するはずです。それが 400 に等しいことをテストしています。

    it(@"should have a return code of 200", ^AsyncBlock{
        [baseService dataWithHttpRequest:testRequest successCallback:^(NSInteger statusCode, NSDictionary *response, NSDictionary *responseHeaders) {
            expect(statusCode).to.equal(400);
            done();
        } errorCallback:^(NSInteger statusCode, NSDictionary *response, NSError *error) {
        }];
    });

ここで、AsyncBlock キーワードを使用して非同期テストを実行し、done() を使用して完了を通知します。私が気づいているのは、Xcode がテストが成功したことを報告していることですが、コンソールのログを見るとわかります

Test Suite 'All tests' passed at 2014-09-07 22:11:22 +0000. Executed 95 tests, with 0 failures (0 unexpected) in 0.489 (0.816) seconds

ロギングを詳しく調べたところ、次のことがわかりました

2014-09-07 23:11:21.480 TwoUpTop[9255:116602] Request <http://www.google.com>
2014-09-07 23:11:21.486 TwoUpTop[9255:116661] http://www.google.com stubbed by My Test Stub
2014-09-07 23:11:21.487 TwoUpTop[9255:116648] NLXApiBaseServicesSpec.m:49 expected: 400, got: 200
Test Case '-[NLXApiBaseServicesSpec   NLXApiBaseServices_Given_that_I_create_a_http_request_with_a_URL_should_have_a_return_code_of_200]' passed (0.012 seconds).

だから今、私は混乱しています-ステータスコードが等しくないと不平を言っています-しかし、テストは成功しています!!!

私が何か間違ったことをしている場合、誰かが何か知っていますか?

役立つ場合、NLXApiBaseService クラスは、defaultSessionConfiguration を使用して NLXApiBaseService クラスを初期化するときに作成される NSURLSessionConfiguration オブジェクトから NSURLSessionDataTask を作成します。

4

1 に答える 1