What's the difference between Arrange and Act in the Arrange, Act, Assert pattern?

1.6k views Asked by At

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?

1

There are 1 answers

2
Dave Schweisguth On BEST ANSWER

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.)