How do you write test for swift nested function?
你如何为swift嵌套函数编写测试?
Since the function is inside another function, I don't know how to call it from outside.
由于该函数在另一个函数内部,我不知道如何从外部调用它。
I'm using nested function because I can't call a private self function in init
我正在使用嵌套函数,因为我无法在init中调用私有自身函数
2 个解决方案
#1
4
You shouldn't test nested function, just like you shouldn't test private methods, from Bad Testing Practices:
你不应该测试嵌套函数,就像你不应该测试私有方法一样,来自Bad Testing Practices:
Private means private. Period. If you feel the need to test a private method, there is something conceptually wrong with that method. Usually it is doing too much to be a private method, which in turn violates the Single Responsibility Principle.
私人意味着私人。期。如果您觉得需要测试私有方法,那么该方法在概念上是错误的。通常,做私人方法做得太多,而后者又违反了单一责任原则。
#2
3
Continuing the point from Code Different, from a testing point of view, the following code is identical:
继续从Code Different开始,从测试的角度来看,以下代码是相同的:
// Without nesting
func f() {
// Do things
// Do other things we want to test
// Do more things
}
// With nesting
func fnest() {
func nest() {
// Do other things we want to test
}
// Do things
nest()
// Do more things
}
Whatever technique you'd use to test "Do other things we want to test" in f()
is precisely the same technique that you'd use to test it in fnest()
. If the piece you want to test is insufficiently accessible, in either case you must redesign (or change your testing goal), but there's nothing special about nested functions.
无论你用什么技术来测试f()中的“我们想要测试的其他东西”,都是用来在fnest()中测试它的技术。如果要测试的部分不够容易访问,在任何一种情况下都必须重新设计(或更改测试目标),但嵌套函数没有什么特别之处。
I'm using nested function because I can't call a private self function in init
我正在使用嵌套函数,因为我无法在init中调用私有自身函数
If this is the entire problem, then make the function static and pure. Rather than passing self
, pass the data required to perform the calculation, and return the result. Then modify self
inside of init
rather than in the function.
如果这是整个问题,那么使函数静态和纯。而不是传递self,传递执行计算所需的数据,并返回结果。然后修改init内部的self而不是函数。
#1
4
You shouldn't test nested function, just like you shouldn't test private methods, from Bad Testing Practices:
你不应该测试嵌套函数,就像你不应该测试私有方法一样,来自Bad Testing Practices:
Private means private. Period. If you feel the need to test a private method, there is something conceptually wrong with that method. Usually it is doing too much to be a private method, which in turn violates the Single Responsibility Principle.
私人意味着私人。期。如果您觉得需要测试私有方法,那么该方法在概念上是错误的。通常,做私人方法做得太多,而后者又违反了单一责任原则。
#2
3
Continuing the point from Code Different, from a testing point of view, the following code is identical:
继续从Code Different开始,从测试的角度来看,以下代码是相同的:
// Without nesting
func f() {
// Do things
// Do other things we want to test
// Do more things
}
// With nesting
func fnest() {
func nest() {
// Do other things we want to test
}
// Do things
nest()
// Do more things
}
Whatever technique you'd use to test "Do other things we want to test" in f()
is precisely the same technique that you'd use to test it in fnest()
. If the piece you want to test is insufficiently accessible, in either case you must redesign (or change your testing goal), but there's nothing special about nested functions.
无论你用什么技术来测试f()中的“我们想要测试的其他东西”,都是用来在fnest()中测试它的技术。如果要测试的部分不够容易访问,在任何一种情况下都必须重新设计(或更改测试目标),但嵌套函数没有什么特别之处。
I'm using nested function because I can't call a private self function in init
我正在使用嵌套函数,因为我无法在init中调用私有自身函数
If this is the entire problem, then make the function static and pure. Rather than passing self
, pass the data required to perform the calculation, and return the result. Then modify self
inside of init
rather than in the function.
如果这是整个问题,那么使函数静态和纯。而不是传递self,传递执行计算所需的数据,并返回结果。然后修改init内部的self而不是函数。