Comments (4)
@krin-san Could you try the commit d8b65f542f6ce2dbcabb58b0b002081d1578b38a
? It should be fixed.
from cuckoo.
@TadeasKriz, the issue with closures now fixed by this commit. However, I catch a few new bugs.
I simplified my real singleton class to smth. like this:
class MyClass {
static let sharedInstance = MyClass()
private init() {}
}
The singleton has no fields to initialize (initialization block is empty) but I want to keep initializer private. Unfortunately, this leads to the following error in auto-generated code:
class MockMyClass: MyClass, Cuckoo.Mock {
typealias Stubbing = __StubbingProxy_MyClass
typealias Verification = __VerificationProxy_MyClass
let manager = Cuckoo.MockManager()
private var observed: MyClass?
// ERROR: Initializer does not override a designated initializer from its superclass
required override init() {
}
...
}
It's very logical. I should keep the initializer as internal or public to make it mockable - and it works! But let's check another case:
class MyClass {
var field: String
init() {
field = ""
}
}
I don't want to just leave an empty internal initializer - it's just meaningless. Now this class contains some field that cannot be initialized by default value so I should write my own initialer. This leads to the following issues in auto-generated code:
class MockMyClass: MyClass, Cuckoo.Mock {
typealias Stubbing = __StubbingProxy_MyClass
typealias Verification = __VerificationProxy_MyClass
let manager = Cuckoo.MockManager()
private var observed: MyClass?
required override init() {
}
required init(spyOn victim: MyClass) {
observed = victim
}
override var field: String {
get {
return manager.getter("field", original: observed.map { o in return { () -> String in o.field } })
}
set {
manager.setter("field", value: newValue, original: observed != nil ? { self.observed?.field = $0 } : nil)
}
}
override init() {
return manager.call("init()", parameters: (), original: observed.map { o in return { () in o.init() } })
}
struct __StubbingProxy_MyClass: Cuckoo.StubbingProxy {
private let manager: Cuckoo.MockManager
init(manager: Cuckoo.MockManager) {
self.manager = manager
}
var field: Cuckoo.ToBeStubbedProperty<String> {
return Cuckoo.ToBeStubbedProperty(manager: manager, name: "field")
}
@warn_unused_result
// ERROR: Expected identifier in function
func init() -> Cuckoo.StubNoReturnFunction<()> {
return Cuckoo.StubNoReturnFunction(stub: manager.createStub("init()", parameterMatchers: []))
}
}
struct __VerificationProxy_MyClass: Cuckoo.VerificationProxy {
private let manager: Cuckoo.MockManager
private let callMatcher: Cuckoo.CallMatcher
private let sourceLocation: Cuckoo.SourceLocation
init(manager: Cuckoo.MockManager, callMatcher: Cuckoo.CallMatcher, sourceLocation: Cuckoo.SourceLocation) {
self.manager = manager
self.callMatcher = callMatcher
self.sourceLocation = sourceLocation
}
var field: Cuckoo.VerifyProperty<String> {
return Cuckoo.VerifyProperty(manager: manager, name: "field", callMatcher: callMatcher, sourceLocation: sourceLocation)
}
// ERROR: Expected identifier in function
func init() -> Cuckoo.__DoNotUse<Void> {
return manager.verify("init()", callMatcher: callMatcher, parameterMatchers: [] as [Cuckoo.ParameterMatcher<Void>], sourceLocation: sourceLocation)
}
}
}
The init
is not a usual mockable function so the func init
syntax is invalid.
from cuckoo.
Thank you for the report. The issue with initializers is currently being worked on so hopefully it is something we can fix. Mocking singletons with private initializers cannot be done as you said. Also we cannot return a different type from the singletons sharedInstance
property, which means you cannot mock and test against it if your code uses singletons.
from cuckoo.
As I already mentioned, singleton and private initializer is not an issue. The issues revealed when I tried to make a class testable.
Normally, in a project I use a singleton instance of MyClass but I allocate it as a usual object while testing.
Looks like the key problem of this issue was resolved and the problem I catch related to #54
from cuckoo.
Related Issues (20)
- Can't generate a mock for protocol where Self: Class
- Why is it a bit like the Chinese I learned in elementary school?
- Protocols extending Sendable lead to concurrency warnings
- Feature Request: add non-testable imports to generator
- Unable to mock functions with certain keywords as argument labels HOT 2
- Use Cuckoo in product target? HOT 2
- Add support for Protocols requiring AnyActor conformance HOT 4
- Permission denied: Can't add comments to the list of files HOT 4
- Strip conflicting imports HOT 2
- Compilation fails in Xcode 14.3
- Error when running Cuckoo/run (cuckoo_generator ver. 1.10.2) HOT 4
- Unable to build with SPM install (ld: symbol(s) not found for architecture arm64) HOT 8
- Cuckoo need XCTest.framework in link binary with Libraries section. otherwise it shows error that is "cannot find 'XCTFail' in scope" in MockManager.swift file HOT 1
- Throwing "'some' types are only permitted in properties, subscripts, and functions" when `func` throws & has `some` for its parameter HOT 2
- Compiler custom flags are not supported by Cuckoo generator HOT 2
- Cuckoo does not support block-type closure HOT 2
- Carthage/Checkouts/Cuckoo/Generator/Generator.xcodeproj' does not exist. HOT 9
- Plenty errors in GeneratedMocks.ts for swift 5.8.1 HOT 2
- Error: Expected member name or constructor call after type name HOT 28
- Issue with Cuckoo Generating Mock for a variable's property HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cuckoo.