can test? • Talking to the actual resource may be OK if it’s stable and fast • Classic versus mockist styles (Martin Fowler) • Solitary versus sociable tests (Jay Fields) • White box versus black box testing • What’s important is the contract http://martinfowler.com/bliki/UnitTest.html
a first time for every developer. Some are more lucky than others because they ramp up in an environment that already embraces unit testing. “But can already write flawless code when I’m in the zone.” True. Because you’re actually running unit tests, without realizing, in your mind when you’re in the zone. Try taking a 3 week break and see what happens to those ephemeral unit tests. Turn those tests into unit test code so that they’re repeatable and unforgettable.
exercise code for the sake of exercising code. • Don’t depend on subsequent tests -- every test runs in its own clean environment, failure of a test doesn’t bring the entire test suite down • Run fast. You need to be able to run all of your tests as quickly and as frequently as possible. Otherwise, they lose value. • Are actually run. Automatically. So that you don’t forget to run them. • Add new unit tests for newly discovered [and fixed] issues.
It all comes down to good architecture and design • Planning for unit tests facilitates good code • Good encapsulation: interfaces with small surfaces, well-defined contracts, non-leaky abstractions • Keep interdependencies low
processes, meeting performance numbers • Testing just for testing: glue code that doesn’t have any logic, ineffective tests that don’t actually test the functionality • Testing legacy code that is actually un-unit-testable Be pragmatic. Don’t waste effort. Sometimes unit testing is not the answer (try end-to-end instead).
code • Safety net for refactoring • Documentation of functionality (especially when in BDD style) • Prevents code from becoming an untestable entangled mass
planning to add test yet • Even if there’s no code worth testing yet • Prime your environment for future unit tests • Especially, CI environment setup can be time consuming • You never know when that moment will come when you have some critical code that needs unit testing Do this. Please.
or energy to write unit tests as you go, prepare a manual test plan, and someone in your team execute them (manually) prior to releases. Bonus: share the effort as a team. Basic smoke tests, checking for end-to-end sanity and regression. Do this. Please.
=> { describe('when called with a callback and a delay', () => { it('should call the callback after the delay', () => { const callback = sinon.spy(); timeout.set(callback, 100); clock.tick(100); expect(callback).to.have.been.called; }); }); });
for an existing account ✓ should return a promise resolved with the account when called for a non-existent account ✓ should return a promise rejected with an error
.catch((error) => { expect(error).to.deep.equal( new Error('Account not found') ); }) .then(() => { throw new Error('Should not have been resolved'); });
for an existing account ✓ should return a promise resolved with the account when called for a non-existent account 1) should return a promise rejected with an error
for an existing account ✓ should return a promise resolved with the account when called for a non-existent account 1) should return a promise rejected with an error AssertionError: expected promise to be rejected with 'Error' but it was fulfilled with { id: -1, name: 'Negative One' }