Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is testing correct $scope initialization an example of "testing someone else's code"?

I'm new to testing, and I understand it's not a good practice to write unit tests that test third-party packages. But I'm curious if the following test would constitute testing the AngularJS framework itself:

describe("TestController", function () {
    it("should set up the $scope correctly", function () {
        expect($scope.foo).toBe("bar");
    });
});

Is it a good idea to test that your $scope got initialized correctly, or since that's AngularJS's job and not yours, is that something you should avoid writing a test for? Sorry if this has an obvious answer, but I want to make sure I'm teaching correct principles when I teach this to my students.

like image 839
bobbyz Avatar asked Mar 31 '26 16:03

bobbyz


1 Answers

In your example, you are testing the behaviour of TestController, which is a class that you wrote, i.e. you are testing your own code.

You are not testing if Angular can set the state properly if you tell it to (which would indeed be a bit redundant, as it is already covered by Angular tests), you are asserting that your code does the things your application requires it to do (that this involves calling Angular functions is secondary).

So that's a good test to write.

Depending on your application, it may be possible to check the same behaviour in a more "high-level" fashion than asserting what exact value a given state variable has. For some applications that could be considered an implementation detail and not the most appropriate way to validate correct behaviour. You should not be testing internal state, but externally visible behavior. In this case, though, since you are testing a controller, and all a controller does is update the state, it's probably appropriate.

If you find that all you are doing in the controller is unconditionally set state, without any logic involved, then you may not really have a need to test the code at that level of granularity (maybe test bigger units that in combination do something "interesting"). The typical example here is testing setter/getter methods: Yes, there is a chance that you get these one-liners wrong, but they make really boring tests, so you might want to skip those (unless they can be automatically generated).

Now, if this test fails, it could be for three (not mutually exclusive) reasons:

1) your code is broken (either some state setup is missing or you are not doing it right). Detecting this is the main purpose of unit testing.

2) Angular is broken (you set the state properly, but somehow Angular lost it). That is unlikely, but if it does happen, you now have a test case to attach to your bug report to Angular. Note that you did not set out to write a test case for Angular, but you got one "by accident".

3) your code as well as Angular are correct, but your test code is wrong. This happens frequently when you update the code that is being tested and test code also needs to be adjusted because its assumptions have been too narrow, or the expected behaviour has changed and the test is now simply outdated.

like image 93
Thilo Avatar answered Apr 03 '26 10:04

Thilo