Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,558
6,058
I've been through about two dozen interviews so far this year (just got my first offer, haven't decided whether to accept it or not yet,) and I've noticed most of them want me to have experience in test driven development. So I decided I'd give it a shot with my next personal project, but I'm having issues with my very first project.

I made a category for NSMutableString which contains a method for counting and removing occurrences of a string. Here's the header:

Code:
#import <Foundation/Foundation.h>

@interface NSMutableString (YRUtilities)
- (NSUInteger)countAndRemoveOccurrencesOfString:(NSString*)string;
- (NSUInteger)countAndRemoveOccurrencesOfString:(NSString*)string inRange:(NSRange)range;
@end

Here's the implementation:

Code:
#import "NSMutableString+YRUtilities.h"

@implementation NSMutableString (YRUtilities)
- (NSUInteger)countAndRemoveOccurrencesOfString:(NSString*)string {
	return [self countAndRemoveOccurrencesOfString:string inRange:NSMakeRange(0, self.length)];
}

- (NSUInteger)countAndRemoveOccurrencesOfString:(NSString*)string inRange:(NSRange)range {
	return [self replaceOccurrencesOfString:string withString:@"" options:0 range:range];
}
@end

Those files are named NSMutableString+YRUtilities.h/m

Here's the test I wrote for them:

Code:
#import "CalcyrTests.h"
#import "NSMutableString+YRUtilities.h"

@implementation CalcyrTests

- (void)setUp
{
    [super setUp];
    
    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.
    
    [super tearDown];
}

- (void)testCountAndRemoveOccurrencesOfString
{
    NSMutableString * string = [@"DeleteDelete Delete DelDeleteete" mutableCopy];
    STAssertEquals(4, [string countAndRemoveOccurrencesOfString:@"Delete"], @"countAndRemoveOccurrencesOfString returned %@", string);
    STAssertEqualObjects(string, @"  Delete", @"countAndRemoveOccurrencesOfString returned %@", string);
}

@end

But I'm getting issues with both parts of that single test.

The first line says "Type mismatch -- countAndRemoveOccurrencesOfString returned DeleteDelete Delete DelDeleteete"

And "'DeleteDelete DelDeleteete' should be equal to ' Delete' countandRemoveOccurrencesOfString returned DeleteDelete Delete DelDeleteete"

I'm confused - what happened? Are my tests failing because I don't know how to write them properly or are they failing because the method I wrote doesn't work?

Edit: Solved. The type mismatch was because I had an integer on the left side and a NSUInteger being returned. All I had to do was add a cast to the 4 to have it be an NSUInteger.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.