简体   繁体   中英

iOS asserting NSMutableArray contains 2 objects, regardless of index

I have the following test case in my iOS application :

-(void) testTwoDefaultUsersExist
{
NSString * expected;
NSString * actual;

expected = @"John Smith";
actual = [[[userService getAllUsers]objectAtIndex:0] fullName];
STAssertEqualObjects(expected, actual, @"Not equal");
expected = @"Dave Brown";
actual = [[[userService getAllUsers]objectAtIndex:1] fullName];
STAssertEqualObjects(expected, actual, @"Not equal");
}

The above just checks that my call to [userService getAllUsers] returns 2 User objects, one with a name of John Smith, the other with Dave Brown. This appears to work fine for this scenario, but I have other cases where that ordering may change, so John may be placed in index 1 rather than 0

Question : How can I assert that the NSMutableArray , being returned from the call to [userService getAllUsers] contains those 2 objects, regardless of ordering?

Can you not simply use the NSArray method -containsObject: ? An NSMutableArray is still an NSArray, so you can do:

NSArray * expected = [NSArray arrayWithObjects:@"John Smith", @"Dave Brown", nil];
NSArray * actual = [[userService getAllUsers] valueForKey:@"fullName"];
for(NSString * name in expected) {
    STAssertTrue([actual containsObject:name], @"Missing name");
}

Note the (ab)use of -valueForKey: to transform an array of user objects into an array of NSString objects, making the -containsObject: call simpler. This will only work if your user object is key-value coding compliant for the fullName property.

  1. NSMutableArray always contains the elements as you insert them to the array

  2. You can iterate over the elements that you insert and test if they're at the NSArray using:

- (NSUInteger)indexOfObject:(id)anObject

If the object is not found it returns NSNotFound, that can be used with the Unit Test Framework that you choice

Greetings

Assert on equality like this

   NSAssert1([userService getAllUsers].count == 2, @"SomeDescription", nil);

If you want to search an array for the existence of some strings, use the following function

- (BOOL) containsAllNames:(NSArray*)arrToSearch namesToSearch:(NSArray*)arr
{    
    BOOL containsAll = YES;

    for (NSString *name in arr) {

        BOOL containsCurrent = NO;
        for (NSString *nameToSearch in arrToSearch) {
            if ([name isEqualToString:nameToSearch]) {
                containsCurrent = YES;
                break;
            }
        }

        if (!containsCurrent) {
            containsAll = NO;
        }
    }

    return containsAll;
}

Call it like

NSArray *toSearch = [NSArray arrayWithObjects:@"John Smith", @"Dave Brown", nil];
[self containsAllNames:YourArray namesToSearch:toSearch];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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