Comments (13)
Pull the latest
from mockolo.
This is currently not supported; contributions are welcome!
Do you want the history to be enabled for all funcs or only for some funcs?
If the former, you can add an input flag (e.g. --enable-args-history) to the commandline, and support it if the flag is enabled.
If the latter, you could utilize arguments to the @mockable annotation (e.g. /// @mockable(history: fooFunc = true; barFunc = true)) and support it if listed in the arguments (see 'typealias', 'rx', 'module' in README for other examples).
from mockolo.
@elsh
Thanks! I'd like to contribute!
Do you want the history to be enabled for all funcs or only for some funcs?
If the former, you can add an input flag (e.g. --enable-args-history) to the commandline, and support it if the flag is enabled.
If the latter, you could utilize arguments to the @mockable annotation (e.g. /// @mockable(history: fooFunc = true; barFunc = true)) and support it if listed in the arguments (see 'typealias', 'rx', 'module' in README for other examples).
I think the former way is better because it's easy to use.
However, do you have any concerns? (e.g. performance)
from mockolo.
Wait can't this just be implemented in the test like so? I don't see the need for Mockolo to have to implement this and it seems overkill given how simple the solution is.
protocol Foo {
func fooFunc(_ arg: Int)
func barFunc(_ arg1: Int, arg2: String)
}
let mock = FooMock()
var fooFuncHistory: [Int] = []
mock.fooFuncHandler = { val in
fooFuncHistory.append(val)
}
mock.fooFunc(1)
mock.fooFunc(2)
mock.fooFunc(3)
XCTAssertEqual(fooFuncHistory, [1, 2, 3])
from mockolo.
@tinder-maxwellelliott
Yes, it's right.
However, we must implement like that many times in some usecases.
For example, my app is built with VIPER architecture and our Presenters output effects to Views/Interactors by calling those functions like as bellow. (In fact, it's more complicated than that.)
In this usecase, using test spy is easy way.
// Protocols
protocol FooView {
func setTitle(_ title: String)
}
protocol FooUsecase {
func getUser(userId: String)
}
protocol FooInteractorDelegate {
func didGetUser(_ user: User)
}
// Presenter
class FooPresenter {
private let view: FooView
private let interactor: FooUsecase
private let userId: String
init(view: FooView, interactor: FooUsecase, userId: String) {
self.view = view
self.interactor = interactor
self.userId = userId
}
}
extension FooPresenter {
func viewDidLoad() {
interactor.getUser(userId: userId)
}
}
extension FooPresenter: FooInteractorDelegate {
func didGetUser(_ user: User) {
view.setTitle(user.name)
}
}
// Test
let mockView = FooViewMock()
let mockInteractor = FooUsecaseMock()
let presenter = FooPresenter(view: mockView, interactor: mockInteractor, userId: "foo_user")
presenter.viewDidLoad()
XCTAssertEqual(mockInteractor.getUserValues, ["foo_user"])
let user = User(id: "foo_user", name: "foo")
presenter.didGetUser(user)
XCTAssertEqual(mockView.setTitleValues, ["foo"])
And some other mock generators support this feature, for example, ArgumentCaptor
in Brightigy/Cuckoo.
I want to use this feature in Mockolo. Maybe I implement this as opt-in.
How about it?
from mockolo.
@elsh
Thanks! I'd like to contribute!Do you want the history to be enabled for all funcs or only for some funcs?
If the former, you can add an input flag (e.g. --enable-args-history) to the commandline, and support it if the flag is enabled.
If the latter, you could utilize arguments to the @mockable annotation (e.g. /// @mockable(history: fooFunc = true; barFunc = true)) and support it if listed in the arguments (see 'typealias', 'rx', 'module' in README for other examples).I think the former way is better because it's easy to use.
However, do you have any concerns? (e.g. performance)
The only concern is that if you enable this for all funcs, there will be more generated code, thus the compile time of mocks will increase as well, so use it at your discretion.
I agree with @tinder-maxwellelliott that this can easily be supported in the test code itself as in the example. However, if you don't want to have to write it for all funcs in the tests and that you have a lot of funcs, we could support this with the optional input flag mentioned above.
from mockolo.
@elsh
Thanks! I'd like to contribute!Do you want the history to be enabled for all funcs or only for some funcs?
If the former, you can add an input flag (e.g. ) to the commandline, and support it if the flag is enabled.
If the latter, you could utilize arguments to the @mockable annotation (e.g. /// @mockable(history: fooFunc = true; barFunc = true)) and support it if listed in the arguments (see 'typealias', 'rx', 'module' in README for other examples).I think the former way is better because it's easy to use.
However, do you have any concerns? (e.g. performance)The only concern is that if you enable this for all funcs, there will be more generated code, thus the compile time of mocks will increase as well, so use it at your discretion.
I agree with @tinder-maxwellelliott that this can easily be supported in the test code itself as in the example. However, if you don't want to have to write it for all funcs in the tests and that you have a lot of funcs, we could support this with the optional input flag mentioned above.
OK, I also agree with @tinder-maxwellelliott and worry about the compile time of mocks.
So I suggest the flexible way as bellow.
- By default, generate argument capture with only annotated funcs. (e.g.
/// @mockable(history: fooFunc = true; barFunc = true)
) - if passed
--enable-args-history
args, generate argument captures with all funcs and ignore annotations.
How about it?
from mockolo.
from mockolo.
@elsh Thanks! I'm going to start implementing!
from mockolo.
@elsh
Test failed on your latest commit(ec8361f).
Were you about to start something?
from mockolo.
I implemented it in #114!
from mockolo.
@andooown Thanks for implementing this! 👍 It's been merged to master. Check out https://github.com/uber/mockolo/releases/tag/1.2.3.
from mockolo.
@elsh Thanks for your reviews!
from mockolo.
Related Issues (20)
- Incorrect mock generated for composed protocol HOT 2
- support for async properties? HOT 2
- @available attribute on function mocked incorrectly HOT 3
- Consider method that has `some` parameter.
- Support Actor Protocol HOT 6
- Xcode14.2 Fatal error: The operation couldn’t be completed. (SwiftSyntaxParser.ParserError error 1.) HOT 1
- Contributors with admin access
- [bug] Duplicated underlying var declarations generated for protocol mocks with init requirements HOT 2
- Allow output to stdout
- SwiftSyntax 508.0.0 HOT 1
- [BUG] Mocking functions with `for` parameter + include-history
- [BUG] Does not mock protocol requirements from other module HOT 2
- import collection is overly broad
- [Bug] Protocol that inherits from protocols in two different modules with same function name does not produce unique handlers HOT 2
- Mock of class with computed properties causes compile error.
- Add build tool plugin HOT 2
- Uncompilable mock generated for NSObjectProtocol / @objc protocols
- Async functions are unsafe in generated mocks, and can crash if used from multiple threads simultaneously HOT 2
- Skip creation of empty files if there are no mocks to generate HOT 2
- Suppress Sendable protocol warnings HOT 3
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 mockolo.