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

Fake Implementation for Segment Analytics #770

Open
saadfarooq opened this issue Jun 30, 2021 · 3 comments
Open

Fake Implementation for Segment Analytics #770

saadfarooq opened this issue Jun 30, 2021 · 3 comments
Labels
improvement This has been identified as a feature improvement needs info This issues requires more information before it can be triaged.

Comments

@saadfarooq
Copy link

Is there a way to Fake the Analytics object for tests? We prefer Faking to Mocking, but the Analytics object uses a builder and it's constructor takes a thousand parameters.

Would be nice if there was a module with a fake implementation to use for tests that allowed assertions on the values passed.

@prayansh
Copy link
Contributor

Hi @saadfarooq! This is a great call :). Definitely seems like a great feature. Would you happen to have any resources / examples around this topic? I am unfamiliar with it so would love to grab some extra info here.

@prayansh prayansh added improvement This has been identified as a feature improvement needs info This issues requires more information before it can be triaged. labels Jun 30, 2021
@saadfarooq
Copy link
Author

saadfarooq commented Jul 1, 2021

Hey Prayansh,

So the semantic difference between mocks and fakes is that mocks test the implementation and fakes test the outcomes.

A mock would verify that our code called a method on it.

verify(mockAnalytics).track("someAction", Properties().apply { put(CATEGORY, "someCategory") })

A fake, on the other hand, would test if a value was received in some limited implementation.

fakeAnalytics.assertAction("someAction")
    .assertValue(CATEGORY, "someCategory")

The mock exposes the implementation details such as that the function is called track, but more importantly that it uses Properties object to bundle values so the tester needs to know if the Properties object supports value equality, for example.

The downside with fakes is that they usually have to be planned out and written, as opposed to mocks, which usually are taken care of by a mocking library.

Also, the design problem with writing fakes for you guys will be how to design the API, e.g. should it include assertions (and thus possibly an assertion library dependency) like RxJava's TestObserver or just provide values for users to assert like AndroidX's navigation testing. (Note: despite the naming these are basically fakes).

The main problem with the library right now is that the constructor takes too many parameters and also uses context. Otherwise, I would be able hack my way to a fake (by doing class FakeAnalytics: Analytics(mock(), mock()..... mock())) but because it uses SharedPreferences upon initialization, mocking context doesn't work and it seems too much to be stubbing that out in the mock.

Hope that was helpful.

@prayansh
Copy link
Contributor

prayansh commented Jul 6, 2021

Hey @saadfarooq! thanks for the info (very helpful 😄 ). This does seem like an interesting feature. I will add it to our backlog, but I cannot promise any immediate progress, since we have other projects in flight currently.

I will update this issue once the status changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement This has been identified as a feature improvement needs info This issues requires more information before it can be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants