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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế