我正在尝试为json验证编写单元测试(因为app严重依赖于来自其余API的json).
我有一个包含简单json的本地文件:"goodFeaturedJson.txt"
内容:
{
"test": "TEST"
}
Run Code Online (Sandbox Code Playgroud)
测试用例:
- (void)testJsonIsValid
{
Featured *featured = [Featured new];
NSString* filepath = [[NSBundle mainBundle]pathForResource:@"goodFeaturedJson" ofType:@"text"];
NSData *data = [NSData dataWithContentsOfFile:filepath];
NSString *jsonString = [[NSString alloc] initWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];//[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"The json string is: %@", jsonString);
id JSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
STAssertTrue([featured jsonIsValid:JSON], @"Featured Data is NOT valid...");
}
Run Code Online (Sandbox Code Playgroud)
每次测试失败.控制台打印:
The json string is: (null)
Run Code Online (Sandbox Code Playgroud)
为什么?我知道为什么测试失败了,因为很明显,如果数据是nil/null,那么就没有有效的json,并且验证器将会中断(如果它无效则应该中断).
我必须在这里找到一些简单的东西,任何想法?
不确定标题是否措辞正确,或者是否有更好的说法,但我认为没关系.
无论如何,到目前为止我理解以下内容:
a.b("a", "b", "c", foo);
Run Code Online (Sandbox Code Playgroud)
其中"foo"是其他地方定义的函数,它不接受任何参数,只会导致函数ab()运行,并带有上述参数.然后可以在函数ab()内调用参数"foo",简单地称为"foo()".换句话说,我理解上面的调用是使用函数指针作为函数ab中的参数作为参数.
好的,现在这就是我要做的......
我想能够做类似于上面的事情,除了这次我希望foo在该参数中传递一个参数,如下所示:
a.b("a", "b", "c", foo("bar"));
Run Code Online (Sandbox Code Playgroud)
现在这是问题所在.这将导致使用参数"a","b","c"和foo("bar")的结果.我不想要这个.我希望foo("bar")按字面顺序传入,以便在函数ab中看起来像这样(作为标题):
a.b(first, second, third, fourth);
Run Code Online (Sandbox Code Playgroud)
可以引用并调用第四个参数:
fourth();
Run Code Online (Sandbox Code Playgroud)
即使"第四"中有一个论点.我似乎无法找到解决这个问题的方法,有什么建议吗?
谢谢!
首先说我见过这些问题:
第一个和最后一个看起来与我的问题非常相关,但是我相当确定每个部分都有逻辑来确定单元格中应该出现什么(数据),但它们仍然混淆了.
以下是相关代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Note: the if (cell == nil) thing is no longer required in iOS 6
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (closestRenter != nil)
{
NSLog(@"CLOSEST RENTER!");
[self setupCellsWithClosestRenterCell:cell atIndexPath:indexPath];
}
else
{
NSLog(@"NO CLOSEST RENTER");
[self setupCellsWithNoClosestRenterCell:cell atIndexPath:indexPath];
}
if (indexPath.section == 0)
{
for (UIView *view in cell.contentView.subviews)
{ …Run Code Online (Sandbox Code Playgroud) 一直在遇到似乎是同源政策,这引起了一些令人头疼的问题!
为了减少追逐,我基本上只是在提供用户名时尝试获取用户的steam64id.
例如,我的用户名:"Emperor_Jordan"我会去:
http://steamcommunity.com/id/emperor_jordan?xml=1
我需要的蒸汽就在顶部.所以我想我会使用JQuery Ajax来获取它并解析出我以后需要的id(steamapi用法需要steam64id),如下所示.以下是相关代码的片段:
$.ajax({
url: "http://steamcommunity.com/id/emperor_jordan/?xml=1",
datatype: "xml",
complete: function()
{
alert(this.url)
},
success: parse
});
function parse(xml)
{
alert("parsing");
_steamID = $(xml).find("steamID64").text();
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是当我收到完成警报时,我从未看到"解析".永远.它永远不会得到回调,这让我相信我遇到了SOP(同源策略).
我是以错误的方式接近这个,是否有解决方法?
谢谢!
拥有我认为应该是一个相对容易的问题来处理成为一个主要的痛苦......我正在尝试做:
a.b("param", function(data)
{
logger.debug("a.b(" + data.toString() + ")");
if (data.err == 0)
{
// No error, do stuff with data
}
else
{
// Error :( Redo the entire thing.
}
});
Run Code Online (Sandbox Code Playgroud)
我的方法是尝试:
var theWholeThing = function() {return a.b("param", function(data)
{
logger.debug("a.b(" + data.toString() + ")");
if (data.err == 0)
{
// No error, do stuff with data
}
else
{
// Error :( Redo the entire thing.
theWholeThing();
}
})};
Run Code Online (Sandbox Code Playgroud)
上面的问题是前者确实有效(除非发生错误时没有处理),后者根本不打印日志消息......就好像“theWholeThing()”调用没有像我认为的那样工作(再次调用整个事情)。
这里一定有什么微妙的错误,有什么提示吗?
谢谢!
javascript ×3
function ×2
ios ×2
objective-c ×2
ajax ×1
jquery ×1
json ×1
parsing ×1
steam ×1
uitableview ×1