HTML and CSS Reference
In-Depth Information
var dds = this.element.getElementsByTagName("dd");
assertEquals(1, dds.length);
assertEquals("We are one", dds[0].innerHTML);
}
Although this test clearly states what happens when the addMessage method
is called, it may not be immediately clear that this.element is associated with
the controller by having been set through setView . Making the situation worse,
we did not write a test that describes the fact that without first calling setView with
a DOM element, the addMessage method is not able to do anything useful—a
fact that is not visible from the test in question either.
We could improve the readability of the test by referring to the element as
this.controller.view instead, but keeping the setView call inside the
test probably yields the best readability. What other changes would you suggest to
improve this test's readability in stand-alone mode?
17.2 Tests as Behavior Specification
When writing unit tests as part of test-driven development, we automatically treat
tests as a specification mechanism—each test defines a distinct requirement and lays
out the next goal to reach. Although we might want to occasionally pick up speed
by introducing more code than “the smallest possible amount of test necessary to
fail the test,” doing so inside one and the same test rarely is the best choice.
17.2.1 Test One Behavior at a Time
Any given unit test should focus clearly on one specific behavior in the system. In
most cases this can be directly related to the number of asserts, or if using mocks,
expectations. Tests are allowed to have more than a single assert, but only when all
the asserts logically test the same behavior. Listing 17.9 revisits a previous example
of a test that uses three assertions to verify one behavior—that calling dispatch
on the Comet client causes the observer to be notified of the right event and with
the right data.
Listing 17.9 Verifying one behavior with three asserts
"test dispatch should notify observers": function () {
var client = Object.create(ajax.cometClient);
client.observers = { notify: stubFn() };
 
 
Search WWH ::




Custom Search