I don't really understand the difference between the arrange and act part of the pattern in unit tests. Does Arrange ONLY mean the creation of the objects? Why should we separate the Arrange from the Act part in the first place, and what's the criteria to decide if something belongs to the Act and not to the Arrange part? To me it seems that everything belongs to the Arrange part, since we "arrange the test" for the assertion, don't we?
-
1Arrange is not only mean the creation of the objects. Arrange setup everything needed for the running the tested code. This includes any initialization of dependencies, mocks and data. – kalkanbatuhan Aug 08 '15 at 11:49
-
Can you circumvent the Arrange from the Act part? – cobby Aug 08 '15 at 11:57
1 Answers
A unit test tests a single "Act" in a program, typically a single method call on an object instance. Arrange, Act, Assert organizes a unit test into three parts: before, during and after the Act.
The Arrange part is everything up to, but not including, the method call of interest. In the Arrange part, we set up the state that we want the world (the object that we're calling the method on, other objects that it interacts with, etc.) to be in when we call the method.
The Act is the call of the method we're testing.
And (to be complete), Assert is the rest of the test, where we Assert that the Act had the effects on the world that we expect.
So we don't "arrange the test for the assertion", we Arrange the world for the Act. In the Arrange part, we do things whose effects we already know. Every method called in the Arrange part should be unit-tested elsewhere. In the Act, we do something whose effect we don't know yet; this is what the test is actually about. (In test-driven development we might not have written the method yet, or added to its implementation to pass this particular test.)

- 36,475
- 10
- 98
- 121
-
1Good answer, thanks! However, this seems that the Act part is really just an one-liner, namely the method call. So I guess the separation is simply for the sake of readability? – cobby Aug 08 '15 at 22:09
-
1Readability, yes, but it's deep readability. The Arrange, Act and Assert parts have very different roles in a test, so separating them is a powerful way to think correctly about the test. – Dave Schweisguth Aug 08 '15 at 23:29