Xcode单元测试比较两个数组参数

huangapple go评论59阅读模式
英文:

Xcode unit test compare two array parameters

问题

这是您要翻译的内容:

我在我的应用程序中有一个流程,它将Response对象插入到一个数组中。这是响应对象:

@interface AppResponse : NSObject

@property (nonatomic, assign) MotSDKState state;
@property (strong, nonatomic) NSError * _Nullable error;
@property (nonatomic, assign) NSInteger errorCode;
@property (strong, nonatomic) NSDictionary * _Nullable response;

@end

我想在流程结束时检查数组是否正确,所以我创建了一个测试:

var err = AppResponse()
err.errorCode = 1000
err.state = .failed
let expectedArray = [err]
XCTAssertEqual(self.responsesArray, expectedArray)

我还尝试了Nimble:

expect(self.responsesArray).to(equal(expectedArray))

但无论哪种方法,我都得到了这个单元测试失败的错误:

error: -[****.**** *****] : failed - expected to equal <[<Appesponse: 0x600003288240>]>, got <[<Appesponse: 0x6000032948d0>]>

有什么想法是什么问题?我如何比较两个对象数组?
英文:

I have a flow in my app that inserts Response objects into an array, This is the response object:

@interface AppResponse : NSObject

@property (nonatomic, assign) MotSDKState  state;
@property (strong, nonatomic) NSError * _Nullable error;
@property (nonatomic, assign) NSInteger errorCode;
@property (strong, nonatomic) NSDictionary * _Nullable response;

@end

I want to check at the end of the flow if the array is correct, so I create a test:

var err = AppResponse()
err.errorCode = 1000
err.state = .failed
let expectedArray = [err]
XCTAssertEqual(self.responsesArray, expectedArray)

I also tried with Nimble:

expect(self.responsesArray).to(equal(expectedArray))

And I get for both of them this unit test failed error:

error: -[****.**** *****] : failed - expected to equal <[<Appesponse: 0x600003288240>]>, got <[<Appesponse: 0x6000032948d0>]>

Any idea what is the problem? how I can compare two arrays of objects?

答案1

得分: 2

注意

我假设我们看到了大部分的AppResponse代码,而且你没有实现[AppResponse isEqual:]方法。如果你已经实现了:

  • 这个答案不适用
  • 你可能应该在问题中包括它

答案

考虑一下这段Swift代码(这可能是你尝试的第一件事情):

let a = AppResponse()
let b = AppResponse()

print(a == b) // 输出 "false"
print(a == a) // 输出 "true"

在幕后,这最后两行等同于:

print(a.isEqual(b))
print(a.isEqual(a))

这是Objective-C的相等方法,[NSObject isEqual:](参见这里)。

该方法的默认实现只是比较对象的指针,而不是它们的内容。正如我们上面所展示的,除非你比较完全相同的对象,否则它将始终为false。

如果你希望这些比较起作用,你需要一个[AppResponse isEqual:]方法,该方法通过它们的属性比较AppResponse对象。因为你在数组中使用它们,我认为这也意味着需要一个符合标准的[AppResponse hash]方法。有很多指南可以帮助你,例如这个

英文:

Caveat

I'm assuming we're seeing most of the AppResponse code, and that you haven't implemented an [AppResponse isEqual:] method. If you have:

  • this answer doesn't apply
  • you possibly should include it with the question

Answer

Consider this Swift code (which maybe should have been the first thing you tried):

let a = AppResponse()
let b = AppResponse()

print(a == b) // prints "false"
print(a == a) // prints "true"

Behind the scenes, these last two lines are equivalent to:

print(a.isEqual(b))
print(a.isEqual(a))

which is the Objective-C equality method, [NSObject isEqual:] (see here).

The default implementation of that method just compares the pointers to the objects, not their contents. As we showed above, it will always be false unless you compare literally the same object.

If you want these comparisons to work, you need an [AppResponse isEqual:] method comparing AppResponse objects by their properties. Because you are using these in arrays, I think that also implies needing a compliant [AppResponse hash] method. There are many guides to help you with that, e.g. this one.

huangapple
  • 本文由 发表于 2023年2月19日 17:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定