0

I have to admit I'm quite new to unit testing and there is a lot questions for me. It's hard to name it but I think it's behaviour test, anyways let me go straight to the example:

I need to test users roles listing to make sure that my endpoint is working correctly and returns all user roles assigned to him.

That means:

  • I need to create user
  • I need to create role
  • I need to assign created role to created user

As we can see there is three operations that must be excecuted before actual test and I believe that in larger applications such list can grow to much larger number of operations and even complex.

The question is how I should test such endpoints: should I just insert raw data to DB, write some code blocks that would do such preparations.

  • 3
    First thing: get clear on terminology. A **unit** test should test a *single* unit in **isolation**. What you are describing here is rather a function or integration test. Something that talks to a real database is **never** a unit test. (yes, the definition of "unit testing" is actually pretty broad - but again: the majority of people has a much more narrow view on this term). And beyond that, I find your question "too broad". We help with specific programming questions - not with outlinging the full strategy of a large test suite. – GhostCat Aug 14 '17 at 07:30

1 Answers1

0

It's probably best if you test the individual units of your service without hitting the service itself, otherwise you're also unit testing the WebApi framework itself.

This will also allow you to mock your database so you don't have to rely on any stored data to run your tests or the authorization to your service.

vc 74
  • 37,131
  • 7
  • 73
  • 89