将结构与 OCMock 或 Hamcrest 一起使用
我遇到了障碍,我想知道这里聪明的集体智慧是否可以提供帮助。 在 ObjC CocoaTouch 中,我试图模拟一个接受结构参数并返回结构的对象。 OCMock 咳出一个毛球,所以我尝试用 Hamcrest 匹配器包裹。 不死。 我正在测试的函数/方法看起来像这样:
- (CLLocationCoordinate2D)pixelToLatLong:(CGPoint)aPoint;
我使用这样的代码:
#define OCMOCK_STRUCT(atype, variable) [NSValue value:&variable withObjCType:@encode(atype)]
-(void) testMyWidget
{
CLLocationCoordinate2D ulLL = (CLLocationCoordinate2D){123,456};
CLLocationCoordinate2D lrLL = (CLLocationCoordinate2D){654,321};
[[[(id)myObj expect] andReturn:OCMOCK_STRUCT(CLLocationCoordinate2D, ulLL)] pixelToLatLong:(CGPoint){0,0}];
[[[(id)myObj expect] andReturn:OCMOCK_STRUCT(CLLocationCoordinate2D, lrLL)] pixelToLatLong:(CGPoint){320,460}];//lower right point
}
这有点有效。 因此,在我正在测试的对象中,我进行了必要的编辑以在构建信息窗口中获得绿色条...错误..绿色按钮。 当我确定我的测试应该通过时,我会收到断言失败错误。 这些错误告诉我该方法被意外调用,并将这些结构的值列为问号。 我尝试用 Hamcrest 匹配器包装结构,但一无所获。 我正准备打破我的调试器,这无疑会告诉我出了什么问题。 这里有人在 OCMock/Hamcrest 和结构方面遇到过类似的问题吗? 如果是这样,处理这些类型的最佳方法是什么?
I'm hitting a road block and I'm wondering if the brilliant collective minds here can help. In ObjC CocoaTouch I'm trying to mock an object that takes struct parameters and returns a struct. OCMock is coughing up a hair-ball so I tried wrapping with a Hamcrest matcher. No die. The function/method I'm testing looks something like this:
- (CLLocationCoordinate2D)pixelToLatLong:(CGPoint)aPoint;
I use code like this:
#define OCMOCK_STRUCT(atype, variable) [NSValue value:&variable withObjCType:@encode(atype)]
-(void) testMyWidget
{
CLLocationCoordinate2D ulLL = (CLLocationCoordinate2D){123,456};
CLLocationCoordinate2D lrLL = (CLLocationCoordinate2D){654,321};
[[[(id)myObj expect] andReturn:OCMOCK_STRUCT(CLLocationCoordinate2D, ulLL)] pixelToLatLong:(CGPoint){0,0}];
[[[(id)myObj expect] andReturn:OCMOCK_STRUCT(CLLocationCoordinate2D, lrLL)] pixelToLatLong:(CGPoint){320,460}];//lower right point
}
That kinda works. So in my object that I'm testing I make the necessary required edits to get a green bar... err.. green button in the build info window. When I'm certain that my test should pass I get assertion failed errors. The errors inform me that the method was invoked unexpectedly and lists the values for these structs as question marks. I tried wrapping the structs with Hamcrest matchers but I'm getting nowhere. I'm getting ready to break out my debugger which will no doubt show me what's wrong. Has anybody here had similar trouble with OCMock/Hamcrest and structs? If so, what's the best way to handle these types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你们非常接近。 你的#define 应该是:
You're very close. Your #define should be:
最好的答案实际上是克里夫本人:http://codeforfun。 wordpress.com/2009/02/07/ocmock-return-a-struct/
他只是没有更新这个问题,羞耻:)
The best answer is actually Cliff's himself: http://codeforfun.wordpress.com/2009/02/07/ocmock-return-a-struct/
He just didn't update this question, shame shame :)
我对宏观答案有疑问; 编写一个返回测试类中的结构的辅助函数并使用:
效果非常好。
I had problems with the macro answer; writing a helper function that returned the struct in the testing class and using:
worked really well.
有时,手动编码的模拟比尝试将模拟对象框架强制超出其正常使用模式更容易。
Sometimes a hand-coded mock is easier than trying to coerce a mock object framework outside of its normal use patterns.