我正在尝试模拟mutableCopy
在 iOS 上使用 OCMock 和 GHUnit 的调用。
尽管测试通过了,但我EXC_BAD_ACCESS
在清理过程中遇到了异常,我正在尝试找出原因。
看看这个。该测试表明可以在 mockmutableCopy
上进行模拟NSString
。在这个测试中,我返回另一个NSString
,而不是一个NSMutableString
. 这只是为了证明mutableCopy
期望被激发,并且测试通过了。
#import <GHUnitIOS/GHUnit.h>
#import "OCMock.h"
@interface TestItClass : GHTestCase @end
@implementation TestItClass
// Test that mutableCopy on an NSString is mockable.
- (void)test_1_mutableCopyOfString_shouldBeMockable_givenAStringIsReturned {
NSString *string = [OCMockObject mockForClass:NSString.class];
NSString *copy = @"foo";
[(NSString *) [[(id) string expect] andReturn:copy] mutableCopy];
// MutableCopy is mocked to return a string, not a mutable string!
// This is clearly wrong from a static typing point of view, but
// the test passes anyway, which is ok.
NSMutableString *result = [string mutableCopy];
GHAssertEquals(result, copy, nil);
[(id)string verify];
}
现在我更改了模拟期望,以便mutableCopy
现在返回一个NSMutableString
. 测试仍然通过,但在测试结束时我得到了一个EXC_BAD_ACCESS
例外。
- (void)test_2_mutableCopyOfString_shouldBeMockable_givenAMutableStringIsReturned {
NSString *string = [OCMockObject mockForClass:NSString.class];
NSMutableString *copy = [@"foo" mutableCopy];
[(NSString *) [[(id) string expect] andReturn:copy] mutableCopy];
// Now mutableCopy is mocked to return a mutable string!
// The test now blows up during the test teardown! Why?
NSMutableString *foo = [string mutableCopy];
GHAssertEquals(foo, copy, nil);
[(id)string verify];
}
@end
在这两个测试中,验证工作,关于断言。这表明这两个测试都构造良好,并且模拟预期正在按预期触发。但是,第二个测试在拆解时因内存访问错误而失败:
Simulator session started with process 7496
Debugger attached to process 7496
2013-03-11 18:23:05.519 UnitTests[7496:c07] TestItClass/test_2_mutableCopyOfString_shouldBeMockable_givenAMutableStringIsReturned ✘ 0.00s
2013-03-11 18:23:06.466 UnitTests[7496:c07] Re-running: TestItClass/test_2_mutableCopyOfString_shouldBeMockable_givenAMutableStringIsReturned <GHTest: 0x7793340>
Exception: EXC_BAD_ACCESS (code=1, address=0x11dfe3ea))
你能告诉我为什么会发生吗?
谢谢,乔