K
K
klinnov2016-09-13 22:37:30
Test Driven Development
klinnov, 2016-09-13 22:37:30

How to write a unit test correctly?

Hello! I understand tests. It turns out that I work with Laravel framework and AngularJS. Most of my methods return JSON objects.
Well, an example of a standard method:

/**
     * Update company.
     *
     * @param int $id Company id
     * @param Request $request Company data
     * @throws ModelNotFoundException If company is not found
     * @return Response
     */
    public function update(int $id, Request $request): Response
    {
        try{
            $company = Company::findOrFail($id);
            $rules = array(
                'email' => 'required|email|unique:companies,email,' . $request->id. ',id',
                'name' => 'required',
                'address' => 'required',
            );

            $validator = Validator::make($request->all(), $rules);
            if ($validator->fails()) {
                $messages = $validator->messages();

                return response([
                    'error' => $messages
                ], 400);
            } else {
                $company->name = $request->name;
                $company->email = $request->email;
                $company->address = $request->address;
                $company->save();

                return response([
                    'success' => true, 'data' => $company
                ], 200);
            }
        }catch (ModelNotFoundException $e){
            return response([
                'error' => true
            ], 400);
        }
    }

Laravel already has everything you need to write a test (on PhpUnit). But here I can not understand how to write a test correctly.
On the Internet, I see simple examples of tests that check for true false (AssertTrue / AssertFalse) method, etc. Well, what if, for example, Json is returned and the arguments are typed?
Can anyone write a valid test for such a method?
It would help me a lot.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-09-13
@klinnov

1) it's not a unit test, it's an integration test. It will check the "part of the system assembly", in your case the apish. If you were testing through the UI (that is, through angular) - this would be called an end-to-end test (from buttons to the database they say).
2)

On the Internet, I see simple examples of tests that check for true false

Let's think. How would you check this by hand? Can you think of? Now can you write a script that checks this for you? So you know how to write integration tests, it's not very difficult. You just need to not get hung up.
What is essentially important to you when you write an apishka? Most likely, you want to always know that the structure of your answers has not broken. That you didn't accidentally change the name of the field, or you didn't accidentally remove the necessary fields. To do this, there is such a thing as json schema. That is, we take json and check for compliance. Again, for phpunit, you can find ready-made assertions so as not to cut the bike.
Next, what else we might want to check is the status of the code. This is again easy and I think it's in the laravel documentation. You may also want to check the headers, but these are already specific things.
In a word, everything that you want to check - you just check. That's right - it's when it fulfills your needs.
Well, you may want to check the data in the json response. There are already a lot of options out there. For example, I washed down my bike for a partial comparison of JSON (well, I'm not interested in checking everything). You can do this with other solutions, but personally I rarely check such things at this level, because ... well, I have other tests for checking logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question