Fix spies.
This commit is contained in:
parent
c7dd503669
commit
4a2a72fac9
|
@ -49,11 +49,14 @@ public struct Generator {
|
|||
code += ""
|
||||
code += "\(token.accessibility.sourceName)\(token.implementation ? "override " : "")init() {"
|
||||
code += "}"
|
||||
code += ""
|
||||
code += "\(token.accessibility.sourceName)init(spyOn victim: \(token.name)) {"
|
||||
code.nest("observed = victim")
|
||||
code += "}"
|
||||
}
|
||||
code += ""
|
||||
code += "\(token.accessibility.sourceName)func spy(on victim: \(token.name)) -> \(mockClassName(token.name)) {"
|
||||
code.nest {
|
||||
code += "observed = victim"
|
||||
code += "return self"
|
||||
}
|
||||
code += "}"
|
||||
token.children.forEach { generate($0) }
|
||||
code += ""
|
||||
generateStubbing(token)
|
||||
|
|
|
@ -15,8 +15,9 @@ public class MockClassWithAttributes: ClassWithAttributes, Cuckoo.Mock {
|
|||
public override init() {
|
||||
}
|
||||
|
||||
public init(spyOn victim: ClassWithAttributes) {
|
||||
public func spy(on victim: ClassWithAttributes) -> MockClassWithAttributes {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
public struct __StubbingProxy_ClassWithAttributes: Cuckoo.StubbingProxy {
|
||||
|
|
|
@ -16,6 +16,11 @@ class MockClassWithInit: ClassWithInit, Cuckoo.Mock {
|
|||
|
||||
private var observed: ClassWithInit?
|
||||
|
||||
func spy(on victim: ClassWithInit) -> MockClassWithInit {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
struct __StubbingProxy_ClassWithInit: Cuckoo.StubbingProxy {
|
||||
private let manager: Cuckoo.MockManager
|
||||
|
||||
|
|
|
@ -19,8 +19,9 @@ class MockTestedClass: TestedClass, Cuckoo.Mock {
|
|||
override init() {
|
||||
}
|
||||
|
||||
init(spyOn victim: TestedClass) {
|
||||
func spy(on victim: TestedClass) -> MockTestedClass {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
override var readOnlyProperty: String {
|
||||
|
@ -212,8 +213,9 @@ class MockTestedProtocol: TestedProtocol, Cuckoo.Mock {
|
|||
init() {
|
||||
}
|
||||
|
||||
init(spyOn victim: TestedProtocol) {
|
||||
func spy(on victim: TestedProtocol) -> MockTestedProtocol {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
var readOnlyProperty: String {
|
||||
|
|
|
@ -29,8 +29,9 @@ class MockA: A, Cuckoo.Mock {
|
|||
override init() {
|
||||
}
|
||||
|
||||
init(spyOn victim: A) {
|
||||
func spy(on victim: A) -> MockA {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
struct __StubbingProxy_A: Cuckoo.StubbingProxy {
|
||||
|
|
|
@ -19,8 +19,9 @@ class MockA: A, Cuckoo.Mock {
|
|||
override init() {
|
||||
}
|
||||
|
||||
init(spyOn victim: A) {
|
||||
func spy(on victim: A) -> MockA {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
struct __StubbingProxy_A: Cuckoo.StubbingProxy {
|
||||
|
@ -54,8 +55,9 @@ class MockB: B, Cuckoo.Mock {
|
|||
override init() {
|
||||
}
|
||||
|
||||
init(spyOn victim: B) {
|
||||
func spy(on victim: B) -> MockB {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
struct __StubbingProxy_B: Cuckoo.StubbingProxy {
|
||||
|
|
|
@ -12,8 +12,9 @@ class MockEmptyClass: EmptyClass, Cuckoo.Mock {
|
|||
override init() {
|
||||
}
|
||||
|
||||
init(spyOn victim: EmptyClass) {
|
||||
func spy(on victim: EmptyClass) -> MockEmptyClass {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
struct __StubbingProxy_EmptyClass: Cuckoo.StubbingProxy {
|
||||
|
|
|
@ -25,8 +25,9 @@ class MockEmptyClass: EmptyClass, Cuckoo.Mock {
|
|||
override init() {
|
||||
}
|
||||
|
||||
init(spyOn victim: EmptyClass) {
|
||||
func spy(on victim: EmptyClass) -> MockEmptyClass {
|
||||
observed = victim
|
||||
return self
|
||||
}
|
||||
|
||||
struct __StubbingProxy_EmptyClass: Cuckoo.StubbingProxy {
|
||||
|
|
|
@ -109,11 +109,11 @@ Usage of Cuckoo is similar to [Mockito](http://mockito.org/) and [Hamcrest](http
|
|||
|
||||
#### Mock initialization
|
||||
|
||||
Mocks can be created with parameterless constructor. If you want to spy on object instead, pass it as `spyOn` parameter. Name of mock class always corresponds to name of the mocked class/protocol with 'Mock' prefix. For example mock of protocol `Greeter` has a name `MockGreeter`.
|
||||
Mocks can be created with the same constructors as the mocked type. If the type has no init specified then parameterless one is generated. If you want to spy on object you can call `spy(on: Type)' method. Name of mock class always corresponds to name of the mocked class/protocol with 'Mock' prefix. For example mock of protocol `Greeter` has a name `MockGreeter`.
|
||||
|
||||
```Swift
|
||||
let mock = MockGreeter()
|
||||
let spy = MockGreeter(spyOn: aRealInstanceOfGreeter)
|
||||
let spy = MockGreeter().spy(on: aRealInstanceOfGreeter)
|
||||
```
|
||||
|
||||
#### Stubbing
|
||||
|
|
|
@ -12,7 +12,7 @@ import Cuckoo
|
|||
class CuckooFunctionsTest: XCTestCase {
|
||||
|
||||
func testReset() {
|
||||
let mock = MockTestedClass(spyOn: TestedClass())
|
||||
let mock = MockTestedClass().spy(on: TestedClass())
|
||||
stub(mock) { mock in
|
||||
when(mock.countCharacters(anyString())).thenReturn(10)
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ class CuckooFunctionsTest: XCTestCase {
|
|||
}
|
||||
|
||||
func testClearStubs() {
|
||||
let mock = MockTestedClass(spyOn: TestedClass())
|
||||
let mock = MockTestedClass().spy(on: TestedClass())
|
||||
stub(mock) { mock in
|
||||
when(mock.countCharacters(anyString())).thenReturn(10)
|
||||
}
|
||||
|
|
|
@ -110,7 +110,7 @@ class FailTest: XCTestCase {
|
|||
|
||||
func testVerifyNoMoreInteractionsFail() {
|
||||
let error = TestUtils.catchCuckooFail {
|
||||
let mock = MockTestedClass(spyOn: TestedClass())
|
||||
let mock = MockTestedClass().spy(on: TestedClass())
|
||||
|
||||
mock.withOptionalClosure("a", closure: nil)
|
||||
mock.noReturn()
|
||||
|
|
|
@ -32,7 +32,7 @@ class StubFunctionTest: XCTestCase {
|
|||
}
|
||||
|
||||
func testThenCallRealImplementation() {
|
||||
let mock = MockTestedClass(spyOn: TestedClass())
|
||||
let mock = MockTestedClass().spy(on: TestedClass())
|
||||
stub(mock) { mock in
|
||||
when(mock.countCharacters("a")).thenCallRealImplementation()
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ class StubNoReturnFunctionTest: XCTestCase {
|
|||
}
|
||||
|
||||
func testThenCallRealImplementation() {
|
||||
let mock = MockTestedClass(spyOn: TestedClass())
|
||||
let mock = MockTestedClass().spy(on: TestedClass())
|
||||
stub(mock) { mock in
|
||||
when(mock.noReturn()).thenCallRealImplementation()
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ class StubNoReturnThrowingFunctionTest: XCTestCase {
|
|||
}
|
||||
|
||||
func testThenCallRealImplementation() {
|
||||
let mock = MockTestedClass(spyOn: TestedClass())
|
||||
let mock = MockTestedClass().spy(on: TestedClass())
|
||||
stub(mock) { mock in
|
||||
when(mock.withNoReturnThrows()).thenCallRealImplementation()
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ class StubThrowingFunctionTest: XCTestCase {
|
|||
}
|
||||
|
||||
func testThenCallRealImplementation() {
|
||||
let mock = MockTestedClass(spyOn: TestedClass())
|
||||
let mock = MockTestedClass().spy(on: TestedClass())
|
||||
stub(mock) { mock in
|
||||
when(mock.withThrows()).thenCallRealImplementation()
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ class StubbingTest: XCTestCase {
|
|||
}
|
||||
|
||||
func testUnstubbedSpy() {
|
||||
let mock = MockTestedClass(spyOn: TestedClass())
|
||||
let mock = MockTestedClass().spy(on: TestedClass())
|
||||
|
||||
XCTAssertEqual(mock.countCharacters("a"), 1)
|
||||
}
|
||||
|
|
BIN
default.profraw
BIN
default.profraw
Binary file not shown.
Loading…
Reference in New Issue