how verify if a method was called in a concrete class at unit test in swift
how verify if a method was called in a concrete class at unit test in swift
I have this scenario:
class X
init(y: ProtocolA)
func foo()
if(y.isSomething())
methodA()
else
methodB()
func methodA()
// any
func methodB()
// any
class Y : ProtocolA
func isSomething(): Bool return true OR false
i wanna test class X,
i will mock ProtocolA to return in isSomething() method true or false in two different test to know if methodA or methodB was called.
What the best strategy to solve this?
ps: with mockito, using a Spy with verify this is very easy, but with Swift this is very painful
edit:
well, i do this:
first, the parts:
class X init(y: Y)
protocol Y func isSomething() -> Bool
now, the struct for test: mock and spy object
typealias VerifyMethodAssert = (count: Int, parameters: [Any]?, returnn: Any?)
Configurable Mock for Dependency
class YMock : Y
init(configure: Bool)
func isSomething return configure
Spy for concrete class
class XSpy : X
private let y: Y
var verify: [String: VerifyMethodAssert] = [
"methodA()": (count: 0, parameters: nil, returnn: nil)
"methodB()": (count: 0, parameters: nil, returnn: nil)
]
var nothing: [String: Bool] = [
"methodA()": false
"methodB()": false
]
init(y: Y, verify: [String: VerifyMethodAssert]?, nothing: [String: Bool]?)
func methodA()
verify["(#function)"] = (count: verify["(#function)"]!.count + 1, parameters: nil,
returnn: nothing["(#function)"]! ? nil : super.methodA())
func methodB(doNothing: Bool = false)
verify["(#function)"] = (count: verify["(#function)"]!.count + 1, parameters: nil,
returnn: nothing["(#function)"]! ? nil : super.methodB())
and test:
class XTest : QuickSpec
override func spec()
describe("a position view model")
it("test 1")
let y = Y(configure: true)
let x = XSpy(y: y)
x.foo()
expect(1).to(x.verify["methodA()"].count)
expect(0).to(x.verify["methodB()"].count)
it("test 2")
let y = Y(configure: true)
let x = XSpy(y: y)
x.foo()
expect(0).to(x.verify["methodA()"].count)
expect(1).to(x.verify["methodB()"].count)
2 Answers
2
There is no out of the box way as far as I know.
One way to do it is to check a counter:
class X
var countA: Int = 0
var countB: Int = 0
init(y: ProtocolA)
func foo()
if(y.isSomething())
methodA()
else
methodB()
func methodA()
countA += 1
// any
func methodB()
countB += 1
// any
This approach is also suggested here.
In this specific case you can subclass X
and use an associated object to hold invocation counts, you can generalize this if you see yourself using it again and again:
X
final class TestX: X
private struct AssociatedKeys
static var invocations = "(String(describing: type(of: TestX.self)))-invocations"
func invocations(for method: String) -> Int
return invocations[method] ?? 0
private var invocations: [String: Int]
get
return objc_getAssociatedObject(self, &AssociatedKeys.invocations) as? [String: Int] ?? [:]
set
objc_setAssociatedObject( self, &AssociatedKeys.invocations, newValue as NSDictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
override func methodA()
super.methodA()
invocations["methodA"] = (invocations["methodA"] ?? 0) + 1
override func methodB()
super.methodB()
invocations["methodB"] = (invocations["methodB"] ?? 0) + 1
let x = TestX(y: Y())
x.invocations(for: "methodA") //0
x.foo()
x.invocations(for: "methodA") //1
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.