- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 iOS 上使用 OCMock 和 GHUnit 模拟对 mutableCopy
的调用。
尽管测试通过了,但我在清理过程中遇到了 EXC_BAD_ACCESS
异常,我正在尝试找出原因。
看看这个。此测试表明可以在模拟 NSString
上模拟 mutableCopy
。在此测试中,我返回了另一个 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))
你能告诉我为什么会发生这种情况吗?
谢谢,乔
最佳答案
您面临的问题是由 ARC 遵循 Basic Memory Management Rules 的事实引起的.具体来说:
You own any object you create
You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).
因此解决方案是查看调用选择器以确定是否保留
returnValue
。
希望对您有所帮助。
关于ios - 使用 OCMock 意外崩溃,在 NSString 上模拟 'mutableCopy',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345899/
使用 WSDL2ObjC 我得到了很多类,它们是 NSString 的子类。我发现初始化这些类的任何对象的 NSString 值很麻烦。 假设我的类(class)是这样的: @interface mC
当调用 +[NSURL URLWithString:] 时,我有两个构建 URL 的选项: [[@"http://example.com" stringByAppendingPathComponent
我需要删除字符串末尾的空格。我怎样才能做到这一点?示例:如果字符串是 "Hello " 它必须变成 "Hello" 最佳答案 摘自这里的答案:https://stackoverflow.com/a/5
NSString *test = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 如何将此字符串转换为字节? 最佳答案 NSData *bytes = [test dataUsingEn
我对 NSString 变量有疑问。 .h 文件 NSString *strDeleteFilePath; @property (nonatomic,retain) NSString*
我对这个功能有疑问: - (instancetype)initWithCenter:(CLLocationCoordinate2D)center
如果我有方法 - (void) myMethod:(NSString *)string { [Object anothermethodWithString:string]; } 我打电话 [O
ios 中初始化字符串变量的两种方式有什么区别(,adv/disadv)? NSString *var = @"value" 和 NSString *var =[ [NSString alloc] i
我认为这个问题已经足够清楚了,但仍然 - 有什么区别: NSString *string = @"Hello world!"; 和 NSString *string = [[NSString allo
我正在尝试将 NSString 转换为单个字符。当我写 [[NSLocale] currentLocale] objectForKey:NSLocaleDecimalSeparator] 时,我得到
我有指示制作一个前缀方法,该方法在每个位置采用两个字符串,其中 mask = 0 并且第一个字符串 = 第二个字符串,直到不满足这些条件为止,即您的前缀 NSString。 我做了尝试,但由于某种原因
我有 3 个 NSString 和 1 个带值的 NSArray,我尝试将所有字符串转换为一个: string01 = [string01 stringByAppendingString:[numbe
我有一个包含以下文本的 NSString: You can now download "file.png" from "http://www.example.com/RANDOM.png" 如何使用该
我有一个完整的 URL,比方说 http://www.mywebsite.com//Folder /Detals /Final Image /La Image Logo.jpg 在这个NSString
在某些情况下,我可能有一个包含多个单词的 NSString,其中一些是重复的。我想要做的是采用如下所示的字符串: One Two Three Three Three Two Two Two One O
所以我有一个 NSString,让我们说:NSString *mainString = @"My Name is Andrew (I like computers)";我想从 mainString 中
这是我的职责。首先 println 将正确的散列打印到控制台,但在下一行程序崩溃。你能帮助我吗? func sha256(string: NSString) -> NSString { var
我刚刚看到一行由同事编写的代码,我以前从未在 Obj-C 中见过它。它看起来形式很糟糕,我想更改它,但它似乎有效(应用程序按预期工作),我什至不知道将其更改为什么。 NSString **string
我花了很长时间寻找一种获取 nsstring 对象的匹配计数的方法。但我找不到。如何获取 String_one 和 String_Two 的匹配计数?我需要你的帮助.. NSString *Strin
在我看来,我正在处理的代码应该是这样写的: if (!instructions) { instructions = [[UITextView alloc] init]; ins
我是一名优秀的程序员,十分优秀!