by xocobola » 12 Mar 2011, 15:38
Hi
I'm new with OCMock, a good piece of software BTW. I realized that a failure in the verification of a mock halts the execution of the test runner, caused by the exception raised in the [verify] method. It wouldn't be better to just log the error and continue with the other tests? I wrote a category for OCMockObject to add this behavior. Here is it:
OCMockObjectExtension.h
- Code: Select all
#import <Foundation/Foundation.h>
#import <OCMock/OCMock.h>
@interface OCMockObject (ErrorReporting)
- (void) verifyAndLog;
@end
OCMockObjectExtension.m
- Code: Select all
#import "OCMockObjectExtension.h"
@implementation OCMockObject (ErrorReporting)
- (void) log: (NSString *) message {
[(NSFileHandle *)[NSFileHandle fileHandleWithStandardError] writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
}
-(void) logError:(NSString *)message {
message = [NSString stringWithFormat:@"Unknown.m:0:0 Unknown:0: error: %@\n", message];
[self log:message];
}
- (void) verifyAndLog {
@try {
[self verify];
}
@catch (NSException * e) {
[self logError: [e description]];
}
}
@end
The stacktrace is not printed, but it would be really easy to add.
Please let me know if I'm missing a standard way to accomplish this.
Thank you!
Hi
I'm new with OCMock, a good piece of software BTW. I realized that a failure in the verification of a mock halts the execution of the test runner, caused by the exception raised in the [verify] method. It wouldn't be better to just log the error and continue with the other tests? I wrote a category for OCMockObject to add this behavior. Here is it:
OCMockObjectExtension.h
[code]
#import <Foundation/Foundation.h>
#import <OCMock/OCMock.h>
@interface OCMockObject (ErrorReporting)
- (void) verifyAndLog;
@end
[/code]
OCMockObjectExtension.m
[code]
#import "OCMockObjectExtension.h"
@implementation OCMockObject (ErrorReporting)
- (void) log: (NSString *) message {
[(NSFileHandle *)[NSFileHandle fileHandleWithStandardError] writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
}
-(void) logError:(NSString *)message {
message = [NSString stringWithFormat:@"Unknown.m:0:0 Unknown:0: error: %@\n", message];
[self log:message];
}
- (void) verifyAndLog {
@try {
[self verify];
}
@catch (NSException * e) {
[self logError: [e description]];
}
}
@end
[/code]
The stacktrace is not printed, but it would be really easy to add.
Please let me know if I'm missing a standard way to accomplish this.
Thank you!