Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing entire services as parameter : hard for unit testing ? #2

Open
npvisual opened this issue Apr 8, 2020 · 1 comment
Open

Comments

@npvisual
Copy link

npvisual commented Apr 8, 2020

Hi @srdanrasic !

Been developing my app, following the Binder Architecture, for a little while now. Works great for most parts. Thanks for coming up with that!

There are still a few things that I am still wondering about and would like to revisit my understanding of those topics. So would appreciate your help.

My biggest struggle is around the passing of services to other services, notably in the "Session" service, as illustrated by the init(client:userService:) of the UserService.

This feels heavy and pretty unwieldy for unit testing services individually (i.e. having to mock an entire service as opposed to just the object we're interested in (i.e. the Room in this case).

So I have tried several times to come up with a different approach and not rely on services as parameters to other services. This implies, in this particular example, that in the AuthenticationSession service the init method of the RoomService be fed just the user we're interested in. Something akin to :

        userService = UserService(client)
        userService.currentUser.observeNext { user in
            roomService = RoomService(client, user: user)
        }

or to avoid Cannot assign to property: 'roomService' is a 'let' constant, if we use signals for the services...

        userService = UserService(client)
        userService
            .currentUser
            .value()
            .map { RoomService(self.client, user: $0) }
            .feedNext(into: roomService)

So anytime the authenticated session changes, all the services are recreated with the new user.

This would work great apart from the pesky 'self' captured by a closure before all members were initialized.

Any thoughts on how to make this whole thing with services better?

@srdanrasic
Copy link
Collaborator

Hey @npvisual,

Regarding that concrete example, yes, you are right that RoomService does not necessarily need a dependency on UserService, but it needs a user.

Alternative to your approach would be passing a signal of user so you could do something like RoomService(client, user: userService.currentUser). When doing unit testing it's then easy to mock the inputs by passing a dummy signal: RoomService(testClient, user: Signal(just: testUser))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants