Swift Tca - id property for testing

i have an object that is decodable, i decode my item and create a new UUID() on init, my tests always fail when i try to mock new data for my test, because the UUID is always different in this case, i tried to implement uuid dependency, but i am not sure exactly how to do it currently, what is the right approach when receiving new data and adding it into an array when your object does have id, thanks for help in advance.

Hi @galInial, it would help if you shared a bit of code of where you are creating these types that require a UUID. If you are doing this in a reducer, then you have access to the dependency and would use it there:

struct Feature {
  @Dependency(\.uuid) var uuid


  case .createButtonTapped:
    state.items.append(Item(id: uuid()))

We have lots of examples of this in the demos of the repo, including here in the SyncUps demo app:

hi @mbrandonw thanks for the info, my code structure look like this:

i am not having my uuid in the init like you just showed, i init it inside, it might be the cause i will check it, anyway the problem is only with my id when fetching data.

if you have any more directions for me, please let me know! thanks :)

and these are my functions in the reducer:

Hi @galInial, the problem is in your MediaItem. You shouldn't use @Dependnecy in the initializer and instead should have the id passed in by whoever is constructing the MediaItem. That will allow you to construct a MediaItem in tests to assert against when the specific UUID you need.

1 Like