levibostian / iOSBlanky

My opinionated iOS app boilerplate
MIT License
6 stars 5 forks source link

Better way of fakes #36

Closed levibostian closed 2 years ago

levibostian commented 4 years ago

Let's say we have a struct:

struct FolderItem: Codable, Hashable {
    let id: String
}

And we create a fake generator for testing:

enum FolderItemFakes {
    case randomFile

    var fake: FolderItem {
        switch self {
        case .randomFile:
            return FolderItem(id: "".random(10))
        }
    }
}

To make it more convenient to generate these in tests...

FolderItem.fake.randomFile

I would prefer this to make it more convenient to generate fakes.

levibostian commented 4 years ago

At this time, I like to generate fakes from code instead of JSON file reading. JSON file reading is convenient because it's easy to keep up-to-date and does not require annoying up keep of a Fakes enum, but it lacks features such as saying randomFile to get randomly generated objects. Random is better then static for testing.

levibostian commented 4 years ago

Could use Sourcery for this.

Iterate properties of a struct and generate code that says, "id: String.random" as the type. I can then create extensions and objects to make sure all data types (String, Int, Bool, custom types) all resolve.

This would only work for random. If I want to create realistic instances I would need to manually create these objects. That's ok because those cases are more rare.