Fix spies.

This commit is contained in:
Filip Dolník 2016-09-07 14:37:18 +02:00
parent c7dd503669
commit 4a2a72fac9
17 changed files with 38 additions and 22 deletions

View File

@ -49,11 +49,14 @@ public struct Generator {
code += "" code += ""
code += "\(token.accessibility.sourceName)\(token.implementation ? "override " : "")init() {" code += "\(token.accessibility.sourceName)\(token.implementation ? "override " : "")init() {"
code += "}" 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) } token.children.forEach { generate($0) }
code += "" code += ""
generateStubbing(token) generateStubbing(token)

View File

@ -15,8 +15,9 @@ public class MockClassWithAttributes: ClassWithAttributes, Cuckoo.Mock {
public override init() { public override init() {
} }
public init(spyOn victim: ClassWithAttributes) { public func spy(on victim: ClassWithAttributes) -> MockClassWithAttributes {
observed = victim observed = victim
return self
} }
public struct __StubbingProxy_ClassWithAttributes: Cuckoo.StubbingProxy { public struct __StubbingProxy_ClassWithAttributes: Cuckoo.StubbingProxy {

View File

@ -16,6 +16,11 @@ class MockClassWithInit: ClassWithInit, Cuckoo.Mock {
private var observed: ClassWithInit? private var observed: ClassWithInit?
func spy(on victim: ClassWithInit) -> MockClassWithInit {
observed = victim
return self
}
struct __StubbingProxy_ClassWithInit: Cuckoo.StubbingProxy { struct __StubbingProxy_ClassWithInit: Cuckoo.StubbingProxy {
private let manager: Cuckoo.MockManager private let manager: Cuckoo.MockManager

View File

@ -19,8 +19,9 @@ class MockTestedClass: TestedClass, Cuckoo.Mock {
override init() { override init() {
} }
init(spyOn victim: TestedClass) { func spy(on victim: TestedClass) -> MockTestedClass {
observed = victim observed = victim
return self
} }
override var readOnlyProperty: String { override var readOnlyProperty: String {
@ -212,8 +213,9 @@ class MockTestedProtocol: TestedProtocol, Cuckoo.Mock {
init() { init() {
} }
init(spyOn victim: TestedProtocol) { func spy(on victim: TestedProtocol) -> MockTestedProtocol {
observed = victim observed = victim
return self
} }
var readOnlyProperty: String { var readOnlyProperty: String {

View File

@ -29,8 +29,9 @@ class MockA: A, Cuckoo.Mock {
override init() { override init() {
} }
init(spyOn victim: A) { func spy(on victim: A) -> MockA {
observed = victim observed = victim
return self
} }
struct __StubbingProxy_A: Cuckoo.StubbingProxy { struct __StubbingProxy_A: Cuckoo.StubbingProxy {

View File

@ -19,8 +19,9 @@ class MockA: A, Cuckoo.Mock {
override init() { override init() {
} }
init(spyOn victim: A) { func spy(on victim: A) -> MockA {
observed = victim observed = victim
return self
} }
struct __StubbingProxy_A: Cuckoo.StubbingProxy { struct __StubbingProxy_A: Cuckoo.StubbingProxy {
@ -54,8 +55,9 @@ class MockB: B, Cuckoo.Mock {
override init() { override init() {
} }
init(spyOn victim: B) { func spy(on victim: B) -> MockB {
observed = victim observed = victim
return self
} }
struct __StubbingProxy_B: Cuckoo.StubbingProxy { struct __StubbingProxy_B: Cuckoo.StubbingProxy {

View File

@ -12,8 +12,9 @@ class MockEmptyClass: EmptyClass, Cuckoo.Mock {
override init() { override init() {
} }
init(spyOn victim: EmptyClass) { func spy(on victim: EmptyClass) -> MockEmptyClass {
observed = victim observed = victim
return self
} }
struct __StubbingProxy_EmptyClass: Cuckoo.StubbingProxy { struct __StubbingProxy_EmptyClass: Cuckoo.StubbingProxy {

View File

@ -25,8 +25,9 @@ class MockEmptyClass: EmptyClass, Cuckoo.Mock {
override init() { override init() {
} }
init(spyOn victim: EmptyClass) { func spy(on victim: EmptyClass) -> MockEmptyClass {
observed = victim observed = victim
return self
} }
struct __StubbingProxy_EmptyClass: Cuckoo.StubbingProxy { struct __StubbingProxy_EmptyClass: Cuckoo.StubbingProxy {

View File

@ -109,11 +109,11 @@ Usage of Cuckoo is similar to [Mockito](http://mockito.org/) and [Hamcrest](http
#### Mock initialization #### 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 ```Swift
let mock = MockGreeter() let mock = MockGreeter()
let spy = MockGreeter(spyOn: aRealInstanceOfGreeter) let spy = MockGreeter().spy(on: aRealInstanceOfGreeter)
``` ```
#### Stubbing #### Stubbing

View File

@ -12,7 +12,7 @@ import Cuckoo
class CuckooFunctionsTest: XCTestCase { class CuckooFunctionsTest: XCTestCase {
func testReset() { func testReset() {
let mock = MockTestedClass(spyOn: TestedClass()) let mock = MockTestedClass().spy(on: TestedClass())
stub(mock) { mock in stub(mock) { mock in
when(mock.countCharacters(anyString())).thenReturn(10) when(mock.countCharacters(anyString())).thenReturn(10)
} }
@ -26,7 +26,7 @@ class CuckooFunctionsTest: XCTestCase {
} }
func testClearStubs() { func testClearStubs() {
let mock = MockTestedClass(spyOn: TestedClass()) let mock = MockTestedClass().spy(on: TestedClass())
stub(mock) { mock in stub(mock) { mock in
when(mock.countCharacters(anyString())).thenReturn(10) when(mock.countCharacters(anyString())).thenReturn(10)
} }

View File

@ -110,7 +110,7 @@ class FailTest: XCTestCase {
func testVerifyNoMoreInteractionsFail() { func testVerifyNoMoreInteractionsFail() {
let error = TestUtils.catchCuckooFail { let error = TestUtils.catchCuckooFail {
let mock = MockTestedClass(spyOn: TestedClass()) let mock = MockTestedClass().spy(on: TestedClass())
mock.withOptionalClosure("a", closure: nil) mock.withOptionalClosure("a", closure: nil)
mock.noReturn() mock.noReturn()

View File

@ -32,7 +32,7 @@ class StubFunctionTest: XCTestCase {
} }
func testThenCallRealImplementation() { func testThenCallRealImplementation() {
let mock = MockTestedClass(spyOn: TestedClass()) let mock = MockTestedClass().spy(on: TestedClass())
stub(mock) { mock in stub(mock) { mock in
when(mock.countCharacters("a")).thenCallRealImplementation() when(mock.countCharacters("a")).thenCallRealImplementation()
} }

View File

@ -26,7 +26,7 @@ class StubNoReturnFunctionTest: XCTestCase {
} }
func testThenCallRealImplementation() { func testThenCallRealImplementation() {
let mock = MockTestedClass(spyOn: TestedClass()) let mock = MockTestedClass().spy(on: TestedClass())
stub(mock) { mock in stub(mock) { mock in
when(mock.noReturn()).thenCallRealImplementation() when(mock.noReturn()).thenCallRealImplementation()
} }

View File

@ -26,7 +26,7 @@ class StubNoReturnThrowingFunctionTest: XCTestCase {
} }
func testThenCallRealImplementation() { func testThenCallRealImplementation() {
let mock = MockTestedClass(spyOn: TestedClass()) let mock = MockTestedClass().spy(on: TestedClass())
stub(mock) { mock in stub(mock) { mock in
when(mock.withNoReturnThrows()).thenCallRealImplementation() when(mock.withNoReturnThrows()).thenCallRealImplementation()
} }

View File

@ -32,7 +32,7 @@ class StubThrowingFunctionTest: XCTestCase {
} }
func testThenCallRealImplementation() { func testThenCallRealImplementation() {
let mock = MockTestedClass(spyOn: TestedClass()) let mock = MockTestedClass().spy(on: TestedClass())
stub(mock) { mock in stub(mock) { mock in
when(mock.withThrows()).thenCallRealImplementation() when(mock.withThrows()).thenCallRealImplementation()
} }

View File

@ -44,7 +44,7 @@ class StubbingTest: XCTestCase {
} }
func testUnstubbedSpy() { func testUnstubbedSpy() {
let mock = MockTestedClass(spyOn: TestedClass()) let mock = MockTestedClass().spy(on: TestedClass())
XCTAssertEqual(mock.countCharacters("a"), 1) XCTAssertEqual(mock.countCharacters("a"), 1)
} }

Binary file not shown.