ios - Swift unit tests mock class static method -


i have static method in class

class {     static func mystaticmethod() -> b {         return b()     } }  class b {     func gettitle() -> string {         // functionality here     } } 

in class method want test use like:

func someboolfunc() -> bool {     var b = a.mystaticmethod()     if (b.gettitle() = “hello”) {          return true     }     return false } 

how write mock class this... tried:

class mocka: {     var mytitle:string     // seems incorrect, because didn't override static function     func mystaticmethod() -> mockb {         var b = mockb()         b.title = mytitle         return b     } }  class mockb: b {     var mytitle:string          func gettitle() -> string {          return mytitle     } } 

and in tests wanted use like:

func testexample() {     = mocka     mocka.title = "my title"     // , func should use mocka instead of     someboolfunc() } 

but of course in theory :(

maybe way?

protocol aproto {   static func mystaticmethod() -> bproto }  class a: aproto {   static func mystaticmethod() -> bproto {     return b()   } }  class mocka: aproto {   static func mystaticmethod() -> bproto {     return mockb()   } }  protocol bproto {   func gettitle() -> string }  class b: bproto {   func gettitle() -> string {     return "hello"   } }  class mockb: bproto {   func gettitle() -> string {     return "bye bye"   } }  func someboolfunc(_ aproto: aproto.type = a.self) -> bool {   var b = aproto.mystaticmethod()   if (b.gettitle() == "hello") {     return true   }   return false }  print(someboolfunc()) print(someboolfunc(mocka.self)) 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -