torusresearch / customauth-swift-sdk

Swift SDK that allows applications to directly interact with the Torus Network, similar to how Torus Wallet does.
BSD 3-Clause "New" or "Revised" License
10 stars 17 forks source link

Unit tests and mocking #10

Open simonmcl opened 3 years ago

simonmcl commented 3 years ago

Theres no way to pass in a URLSession into any of the Torus functions. This is how most libraries expose the ability to mock URL requests so that code can be unit tested.

Is there any other method available to mock requests that i've missed?

If not, please update the library to allow me to pass in my own URLSession so I can create my own mocks

simonmcl commented 3 years ago

@rathishubham7

metallicalfa2 commented 3 years ago

The initializer accepts a AbstractFactory, you can use this to mock the SDK APIs on the functional level. Please checkout from the master.

simonmcl commented 3 years ago

@rathishubham7 it would be much easier for people not familiar with the code to just be able to pass in a URLSession instance. Making it so that one of your classes can be passed in, means anyone using it needs to understand the code, subclass it, spend time reading it, learn what all the functions do and how they are used, possibly battle with some critical things being private, have to keep updating it whenever anything changes

Passing in a URLSession means that I can simply make a dictionary of:

[
    URL(string: "<torusURL>"): "stub-filename.json"
]

And have a mock instance simply check for the URL and load the corresponding file if it matches.

e.g.

This allows me to mock 100% of requests being used by my library, no matter where they come from, with a single file, without having to create tonnes of mock instance classes

simonmcl commented 3 years ago

@rathishubham7 also I needed to make all these properties public so that I could access them later inside the app, can you make that change to the main repo too: https://github.com/simonmcl/torus-direct-swift-sdk/blob/a6c7f8e4628b43f1c363c797b9ed5c07a575234c/Sources/TorusSwiftDirectSDK/Models/SubVerifierDetails.swift#L19