Replacing Code with Test Doubles Flashcards

1
Q

What are test doubles?

A

A test double is a replacement for a component that looks and behaves similarly, and provides the same API, yet often is a simplified version thereof.
They can often have hard-coded behaviour configured during test setup.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why use test doubles?

A
  1. Test doubles provide the isolation that unit tests require.
  2. Test doubles provide consistent and expected response every time.
  3. Test doubles are fast because they provide little to none executable code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the variations of test doubles?

A
  1. Test stub
  2. Test spy
  3. Mock object
  4. Fake object
  5. Dummy object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a test stub?

A

A test double that simulates the behavior of a component by taking complete control over the responses and not responding to a request unless programmed to.

A test stub temporarily replaces a real component, so the original can be restored as needed.

A stub can be used to verify indirect input. For example, faking a database query response.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a test spy?

A

A test double that observes interactions with tested code. They are primarily used to count the number of times a component was called, along with how it was called and what it responded with.

Spies verify the indirect outputs of a component, and they can either be added to an existing component in order to see how it behaves, or replace it completely (like a stub).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a mock object?

A

A test double that is preprogrammed with expectations of how something is supposed to work and completely leave out the responses.

A mock verifies behavior. Specifically, the expectations around how something is called, including with what arguments and how many times.

A mock doesn’t return anything, which differentiated it from a stub. They are used to verify the use of the method that’s being tested.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a fake object?

A

A test double that is a working implementation of a component that takes a shortcut, typically for performance reasons. They are not usable in production and are intended only for testing purposes.

Example: replacing a database with an in-memory array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a dummy object?

A

A test double that provides parameters for the function being tested.

Example: testing validation with arbitrary input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In summary, what does a test stub do?

A

Verify indirect inputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In summary, what does a test spy do?

A

Verify indirect outputs for later verification.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In summary, what does a mock object do?

A

Verify indirect outputs against expectations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In summary, what does a fake object do?

A

They are like mock objects in that they verify indirect outputs against expectations, but they run faster with less functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In summary, what does a dummy object do?

A

Specify values used for testing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Sinon?

A

It’s the most popular JavaScript library for test spies, stubs, and mocks. It can be used with any unit testing library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does Sinon create?

A

Spies, stubs, and mocks, as well as XMLHttpRequest for testing Ajax, and even setting up a fake server and more.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is sinon-chai?

A

It’s a node module that extends Chai (the assertion library) with Sinon.js assertions in the ‘should’ and expect styles. It’s not required in order to use Sinon with Chai but it does improve the readability.