Giter Club home page Giter Club logo

Comments (13)

elsh avatar elsh commented on May 14, 2024 1

Pull the latest

from mockolo.

elsh avatar elsh commented on May 14, 2024

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.

andooown avatar andooown commented on May 14, 2024

@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.

tinder-maxwellelliott avatar tinder-maxwellelliott commented on May 14, 2024

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.

andooown avatar andooown commented on May 14, 2024

@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 avatar elsh commented on May 14, 2024

@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.

andooown avatar andooown commented on May 14, 2024

@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.

  1. By default, generate argument capture with only annotated funcs. (e.g. /// @mockable(history: fooFunc = true; barFunc = true))
  2. if passed --enable-args-history args, generate argument captures with all funcs and ignore annotations.

How about it?

from mockolo.

elsh avatar elsh commented on May 14, 2024

from mockolo.

andooown avatar andooown commented on May 14, 2024

@elsh Thanks! I'm going to start implementing!

from mockolo.

andooown avatar andooown commented on May 14, 2024

@elsh
Test failed on your latest commit(ec8361f).
Were you about to start something?

from mockolo.

andooown avatar andooown commented on May 14, 2024

I implemented it in #114!

from mockolo.

elsh avatar elsh commented on May 14, 2024

@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.

andooown avatar andooown commented on May 14, 2024

@elsh Thanks for your reviews!

from mockolo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.