bookshelf icon indicating copy to clipboard operation
bookshelf copied to clipboard

Unit test usecases/stories

Open littlepea opened this issue 4 years ago • 2 comments

It looks like dependency injection lands itself perfectly for testing, so that the business logic (stories) could be tested with injecting dummy callables for data access which would return test entities.

But the project doesn't have any tests, so it doesn't demonstrate how this approach contributes to better testability...

Btw, is it necessary to use attrs for stories or it could be replaced with dataclasses?

littlepea avatar Jun 05 '20 09:06 littlepea

Hi, you are free to use attrs, dataclasses, pydantic, or old good __init__ method together with dependencies. Usually, I override things with Injector.let in tests. I replace only things I can't control like SMS gateways, API calls, SIP, Email, Facebook GraphQL API, etc. A test usually calls stories for preparation, to do actual testing, and to validate the response. So, it would be something like that:

def test_registeration():
    code = implemented.RequestCodeStory.let(twillio=lambda: ...).request(AnonumousUser())
    user = implemented.SubmitConfirmationStory.let(twilio=lambda: ...).submit(code)
    assert user.is_authorized()
  • no factory boy (never)
  • checks only business logic behavior
  • most of the tests are integration tests
  • unit tests can test entities only
  • do not ever test story step separately (delegate to the mapper, function, or a low-level object)

I hope that makes sense.

Have a good day :tada:

Best regards, Artem.

proofit404 avatar Jun 05 '20 19:06 proofit404

Makes perfect sense, thanks, @proofit404 ! :)

I still think it would be great to add a few tests like this to this demo repo so it would speak for itself ;)

littlepea avatar Jun 08 '20 03:06 littlepea