[英]Unit testing strategy for conforming to Equatable
Let's say I have a struct
that conforms to Equatable
for my model, something like this: 假设我有一个符合我的模型
Equatable
的struct
,如下所示:
struct Model: Equatable {
var a: Int = 0
var b: String = ""
}
func ==(lhs: Model, rhs: Model) -> Bool {
return lhs.a == rhs.a && lhs.b == rhs.b
}
Now I write some unit tests for this. 现在我为此写一些单元测试。 Something like:
就像是:
func testModelsAreEqual() {
let model1 = Model()
let model2 = Model()
XCTAssertEqual(model1, model2)
}
func testModelsAreNotEqual1() {
let model1 = Model()
var model2 = Model()
model2.b = "hello world"
XCTAssertNotEqual(model1, model2)
}
func testModelsAreNotEqual2() {
let model1 = Model()
var model2 = Model()
model2.a = 1
XCTAssertNotEqual(model1, model2)
}
But how can I write a test that will protect me from the scenario that another property is added to Model
without being added to the memberwise-equality check for ==
like: 但是,如何编写一个测试来保护我免受另一个属性添加到
Model
情况,而不会添加到==
的成员等式检查中:
struct Model: Equatable {
var a: Int = 0
var b: String = ""
var c: Double = 0
}
func ==(lhs: Model, rhs: Model) -> Bool {
return lhs.a == rhs.a && lhs.b == rhs.b
}
Where obviously my tests will all still pass even though conceptually my Equatable
is broken. 很明显我的测试仍然会通过,即使在概念上我的
Equatable
也被打破了。 Is there a testing strategy I could adopt here that will help alert me to this problem? 我可以采用的测试策略是否有助于提醒我这个问题? Is there something I can do with Swift's
Mirror
and limited reflection? 有什么东西我可以用斯威夫特的
Mirror
和有限的反射? Perhaps Mirror.children.count
? 也许
Mirror.children.count
? Or does anyone have a better suggestion? 或者有人有更好的建议吗?
After searching for other solutions, I have decided to use reflection for member count to alert me of changes. 在搜索其他解决方案之后,我决定使用反射进行成员计数以提醒我更改。 Here is the test for the example above:
以下是上述示例的测试:
func testModelStillHas2Members() {
XCTAssertEqual(Mirror(reflecting: Model()).children.count, 2, "The member count of Model has changed, please check the `==` implementation to ensure all members are accounted for.")
}
This is my take: 这是我的看法:
func testModel3() {
let model1 = Model()
var model2 = Model()
model2.c = 1
model2.p.name = "Jack"
let mirror1 = Mirror(reflecting: model1)
let mirror2 = Mirror(reflecting: model2)
let prop1 = mirror1.children.reduce([String: NSObject]()) {
(var dict, e) in
dict[e.label!] = e.value as? NSObject
return dict
}
let prop2 = mirror2.children.reduce([String: NSObject]()) {
(var dict, e) in
dict[e.label!] = e.value as? NSObject
return dict
}
if model1 == model2 {
XCTAssertEqual(prop1, prop2, "Not really equal")
} else {
XCTAssertNotEqual(prop1, prop2, "Not really diffrent")
}
}
It builds 2 dictionaries with the names & values of all properties in model1
and model2
, then assert if these 2 dictionaries are equal. 它使用
model1
和model2
中所有属性的名称和值构建2个字典,然后断言这两个字典是否相等。
Big warning: I have not thoroughly tested this. 大警告:我没有彻底测试过这个。 It worked for the limited number of cases I threw at it.
它适用于我投入的有限数量的案件。 You may have to improvise from here.
你可能需要从这里即兴发挥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.