by NBO » 09 Jul 2010, 15:26
Hi,
I'm trying to mock a class method.
I'm testing the GReader class, but I don't want to test the GReaderHelper class which is needed by GReader in my example.
For this, I'm using OCMock :
This is my test case file :
[code]
// File GReaderTest.m :
@implementation GReaderTest
...
- (void)testAuthentication
{
id mock = [OCMockObject mockForClass:[GReaderHelper class]];
[[[mock stub] andCall:@selector(fakeAuthenticationPost:)
onObject:self] poster:[OCMArg any]];
// I would like this line to call the fakeAuthenticationPost: method
// AND NOT the original GReaderHelper::poster method, but the mock doesn't work
// and the poster method is called
[gr authenticateWithUsername:@"dan" password:@"password"];
}
- (NSString *)fakeAuthenticationPost:(NSString *)request
{
... // Testing parameter values ...
}
@end
[/code]
And this is the tested file :
[code]
// Tested file : GReader.m
@implementation GReader
- (id)init {
self = [super init];
helper = [[GReaderHelper alloc] init];
return self;
}
- (void)authenticateWithUsername:(NSString *)username password:(NSString *)password
{
[helper poster:[NSString stringWithFormat:@"Email=%@&Passwd=%@", username, password]];
}
@end
// Class an method I WANT TO EXCLUDE from the test (using OCMock) :
@implementation GReaderHelper
- (NSString *)poster:(NSString *)request
{
...
return @"My post data";
}
@end
[/code]
There are no errors BUT MOCKING DOESN'T WORK !
The method authenticateWithUsername: calls the original poster: method, AND NOT THE TEST METHOD fakeAuthenticationPost:
Conclusion : I don't manage to use OCMock in an interesting way ... Does anyone have an idea for me ?
Thank you ...
Hi,
I'm trying to mock a class method.
I'm testing the GReader class, but I don't want to test the GReaderHelper class which is needed by GReader in my example.
For this, I'm using OCMock :
This is my test case file :
[code]
// File GReaderTest.m :
@implementation GReaderTest
...
- (void)testAuthentication
{
id mock = [OCMockObject mockForClass:[GReaderHelper class]];
[[[mock stub] andCall:@selector(fakeAuthenticationPost:)
onObject:self] poster:[OCMArg any]];
// I would like this line to call the fakeAuthenticationPost: method
// AND NOT the original GReaderHelper::poster method, but the mock doesn't work
// and the poster method is called
[gr authenticateWithUsername:@"dan" password:@"password"];
}
- (NSString *)fakeAuthenticationPost:(NSString *)request
{
... // Testing parameter values ...
}
@end
[/code]
And this is the tested file :
[code]
// Tested file : GReader.m
@implementation GReader
- (id)init {
self = [super init];
helper = [[GReaderHelper alloc] init];
return self;
}
- (void)authenticateWithUsername:(NSString *)username password:(NSString *)password
{
[helper poster:[NSString stringWithFormat:@"Email=%@&Passwd=%@", username, password]];
}
@end
// Class an method I WANT TO EXCLUDE from the test (using OCMock) :
@implementation GReaderHelper
- (NSString *)poster:(NSString *)request
{
...
return @"My post data";
}
@end
[/code]
There are no errors BUT MOCKING DOESN'T WORK !
The method authenticateWithUsername: calls the original poster: method, AND NOT THE TEST METHOD fakeAuthenticationPost:
Conclusion : I don't manage to use OCMock in an interesting way ... Does anyone have an idea for me ?
Thank you ...