A simple Xcode extension for generate mock class.
MockGen parse string source code and generate mock. Mostly thanks SwiftSemantics
and SwiftSyntax
MockGen works with Xcode alert. You should first tap fix button when Xcode say Type 'XClass' does not conform to protocol 'XProtocol'
. Then run extension. Extension reads your current file and generate mock.
MockGen doesn't need to read your entire project.
- Download MockGen
- Move MockGen to Applications folder
- Open System Preferences
- Open Extensions and Xcode Source Editor. Activate MockGen
- Spy mock generation
- Simple and fast
- No need access all files
- Powered by Apples open source SwiftSyntax and other.
- MockGen can't determinate variables accessors. All variables generate mocks with
get set
accessors.
ProductProtocol
protocol ProductProtocol {
var name: String { get }
var favoriteStatus: String { get set }
func openImages()
func description() -> String
func addToBasket(completion: (Bool) -> Void)
}
Xcode fixed MockProduct
final class MockProduct: ProductProtocol {
var name: String
var favoriteStatus: String
func openImages() {
<#code#>
}
func description() -> String {
<#code#>
}
func addToBasket(completion: (Bool) -> Void) {
<#code#>
}
}
MockGen generated mock
final class MockProduct: ProductProtocol {
var invokedNameSetter = false
var invokedNameSetterCount = 0
var invokedName: String?
var invokedNameList: [String] = []
var invokedNameGetter = false
var invokedNameGetterCount = 0
var stubbedName: String!
var name: String {
set {
invokedNameSetter = true
invokedNameSetterCount += 1
invokedName = newValue
invokedNameList.append(newValue)
}
get {
invokedNameGetter = true
invokedNameGetterCount += 1
return stubbedName
}
}
var invokedFavoriteStatusSetter = false
var invokedFavoriteStatusSetterCount = 0
var invokedFavoriteStatus: String?
var invokedFavoriteStatusList: [String] = []
var invokedFavoriteStatusGetter = false
var invokedFavoriteStatusGetterCount = 0
var stubbedFavoriteStatus: String!
var favoriteStatus: String {
set {
invokedFavoriteStatusSetter = true
invokedFavoriteStatusSetterCount += 1
invokedFavoriteStatus = newValue
invokedFavoriteStatusList.append(newValue)
}
get {
invokedFavoriteStatusGetter = true
invokedFavoriteStatusGetterCount += 1
return stubbedFavoriteStatus
}
}
var invokedOpenImages = false
var invokedOpenImagesCount = 0
func openImages() {
invokedOpenImages = true
invokedOpenImagesCount += 1
}
var invokedDescription = false
var invokedDescriptionCount = 0
var stubbedDescriptionResult: String!
func description() -> String {
invokedDescription = true
invokedDescriptionCount += 1
return stubbedDescriptionResult
}
var invokedAddToBasket = false
var invokedAddToBasketCount = 0
var stubbedAddToBasketCompletionResult: (Bool, Void)?
func addToBasket(completion: (Bool) -> Void) {
invokedAddToBasket = true
invokedAddToBasketCount += 1
if let result = stubbedAddToBasketCompletionResult {
_ = completion(result.0)
}
}
}
- SwiftSemantics - Forked and updated SwiftSemantics
- SwiftSyntax
- swift-format