forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectOfflineTests.m
99 lines (72 loc) · 2.9 KB
/
ObjectOfflineTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <OCMock/OCMock.h>
@import Bolts.BFTask;
#import "PFObject.h"
#import "PFOfflineStore.h"
#import "PFUnitTestCase.h"
#import "Parse_Private.h"
@interface ObjectOfflineTests : PFUnitTestCase
@end
@implementation ObjectOfflineTests
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
- (id)mockedOfflineStore {
id store = PFStrictClassMock([PFOfflineStore class]);
[Parse _currentManager].offlineStore = store;
return store;
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testFetchFromLocalDatastore {
id store = [self mockedOfflineStore];
PFObject *object = [PFObject objectWithClassName:@"Yarr"];
[OCMExpect([store fetchObjectLocallyAsync:object]) andReturn:[BFTask taskWithResult:nil]];
XCTAssertNoThrow([object fetchFromLocalDatastore]);
OCMVerifyAll(store);
}
- (void)testFetchFromLocalDatastoreWithError {
id store = [self mockedOfflineStore];
PFObject *object = [PFObject objectWithClassName:@"Yarr"];
NSError *expectedError = [NSError errorWithDomain:@"YoloTest" code:100500 userInfo:nil];
[OCMExpect([store fetchObjectLocallyAsync:object]) andReturn:[BFTask taskWithError:expectedError]];
NSError *error = nil;
XCTAssertNoThrow([object fetchFromLocalDatastore:&error]);
XCTAssertEqualObjects(error, expectedError);
OCMVerifyAll(store);
}
- (void)testFetchFromLocalDatastoreViaTask {
id store = [self mockedOfflineStore];
PFObject *object = [PFObject objectWithClassName:@"Yarr"];
[OCMExpect([store fetchObjectLocallyAsync:object]) andReturn:[BFTask taskWithResult:object]];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[object fetchFromLocalDatastoreInBackground] continueWithSuccessBlock:^id(BFTask *task) {
XCTAssertEqual(task.result, object);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
OCMVerifyAll(store);
}
- (void)testFetchFromLocalDatastoreViaBlock {
id store = [self mockedOfflineStore];
PFObject *object = [PFObject objectWithClassName:@"Yarr"];
[OCMExpect([store fetchObjectLocallyAsync:object]) andReturn:[BFTask taskWithResult:object]];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[object fetchFromLocalDatastoreInBackgroundWithBlock:^(PFObject *resultObject, NSError *error) {
XCTAssertEqual(resultObject, object);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForTestExpectations];
OCMVerifyAll(store);
}
@end