seanhenry / MockGenerator

An AppCode plugin to help generate Swift test doubles automatically.
GNU General Public License v3.0
71 stars 11 forks source link

Add ability to check if property accessors were invoked #6

Closed BohdanOrlov closed 7 years ago

BohdanOrlov commented 7 years ago

Hello, Sean! Thank you for the generator. You did an amazing job!

Would it be hard for you to add some missing functionality for mock code generated for properties?

Consider this code and generated mock.

public protocol Album {
    var photoItems: [Photo] { get } 
    func photos() -> [Photo]
}

public final class MockAlbum: Album {
    public var stubbedPhotoItems: [Photo]!
    public var photoItems: [Photo] {
        return stubbedPhotoItems
    }
    public var invokedPhotos = false
    public var invokedPhotosCount = 0
    public var stubbedPhotosResult: [Photo]!
    public func photos() -> [Photo] {
        invokedPhotos = true
        invokedPhotosCount += 1
        return stubbedPhotosResult
    }
}

It would be great that photoItems property also had invokedGet<ProperyName> and invokedGet<ProperyName>Count.

Otherwise, it is not possible to check if the property was invoked.

Accordingly, for { get set } properties it would be amazing to have the same thing for Setter: invokedSet<ProperyName> and invokedSet<ProperyName>Count and invokedSet<ProperyName>Parameter

seanhenry commented 7 years ago

Hi Bohdan. Thanks for raising this issue. I can't see a problem with what you've suggested. I might suggest the following naming with get/set though:

var invokedPhotoItemsGetter = false
var invokedPhotoItemsGetterCount = 0
...
var invokedPhotoItemsSetter = false
var invokedPhotoItemsSetterCount = 0

I'll look into the implementation of this.

seanhenry commented 7 years ago

@BohdanOrlov This is now fixed. Property getter and setters are now treated like methods. So you can check their invocations like this:

var invokedPhotoItemsGetter = false
var invokedPhotoItemsGetterCount = 0
var stubbedPhotoItems: [Photo]!
...
var invokedPhotoItemsSetter = false
var invokedPhotoItemsSetterCount = 0
var invokedPhotoItems: [Photo]?
var invokedPhotoItemsList = [[Photo]]()

It will take a day or 2 for JetBrains to process the new update. If you want it in the meantime you can find the JAR in the releases page.

BohdanOrlov commented 7 years ago

Thank you very much @seanhenry! Will try it out!