theory rspec Flashcards
RSpec
RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications.
red-green loop
- Write the smallest possible test case that matches what we need to program.
- Run the test and watch it fail. This gets you into thinking how to write only the code that makes it pass.
- Write some code to make the test pass.
- Run your test suite. Repeat steps 3 and 4 until all tests pass.
- Go back and refactor your new code, making it as simple and clear as possible while keeping the test suite green.
describe block
We are using another describe block to describe the add class method. By convention, class methods are prefixed with a dot (“.add”), and instance methods with a dash (“#add”).
context block
We are using a context block to describe the context under which the add method is expected to return zero. context is technically the same as describe, but is used in different places, to aid reading of the code.
it block
We are using an it block to describe a specific example, which is RSpec’s way to say “test case”. Generally, every example should be descriptive, and together with the context should form an understandable sentence. This one reads as “add class method: given an empty string, it returns zero“.
expect(…).to and the negative variant expect(…).not_to
expect(…).to and the negative variant expect(…).not_to are used to define expected outcomes. The Ruby expression they are given (in our case, StringCalculator.add(“”)) is combined with a matcher to fully define an expectation on a piece of code. The matcher we are using here is eq, a basic equality matcher.
purposes of specs
1) to drive development, and 2) as a verification mechanism.
Specs Directory Structure
Model specs reside in the spec/models directory
Controller specs reside in the spec/controllers directory
Request specs reside in the spec/requests directory. The directory can also be named integration or api.
Feature specs reside in the spec/features directory
View specs reside in the spec/views directory
Helper specs reside in the spec/helpers directory
Mailer specs reside in the spec/mailers directory
Routing specs reside in the spec/routing directory
Job specs reside in the spec/jobs directory
System specs reside in the spec/system directory
Model specs
A model spec is a thin wrapper for an ActiveSupport::TestCase, and includes all
of the behavior and assertions that it provides, in addition to RSpec’s own
behavior and expectations.
Controller specs
It allows you to simulate a single http request in each example, and then
specify expected outcomes such as:
rendered templates
redirects
instance variables assigned in the controller to be shared with the view
cookies sent back with the response
Request spec
Request specs provide a thin wrapper around Rails’ integration tests, and are
designed to drive behavior through the full stack, including routing
(provided by Rails) and without stubbing (that’s up to you).
With request specs, you can:
specify a single request
specify multiple requests across multiple controllers
specify multiple requests across multiple sessions
Feature spec
Feature specs are high-level tests meant to exercise slices of functionality
through an application. They should drive the application only via its external
interface, usually web pages.
View spec
Use them to test the content of view templates
without invoking a specific controller.
Helper spec
Helper specs expose a helper object, which includes the helper module being
specified, the ApplicationHelper module (if there is one) and all of the
helpers built into Rails. It does not include the other helper modules in
your app.
To access the helper methods you’re specifying, simply call them directly
on the helper object.
NOTE: helper methods defined in controllers are not included.
Mailer specs
A mailer spec is a thin wrapper for an ActionMailer::TestCase, and includes all
of the behavior and assertions that it provides, in addition to RSpec’s own
behavior and expectations.