Rspec Flashcards

1
Q

What is Rspec?

A

It is a testing Framework for automated tests

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

What does Rspec stand for?

A

Ruby Specifications

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

What is the structure of a spec?

A
  • Given some context
  • When some event occurs
  • Then I expect some outcome

Given-When-Then

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

How is RSpec structured

A
  • It uses ruby but has a DSL (domain specific language) meaning that it has its own way of doing things
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What reasons are there to test?

A
  • Find bugs
  • Stimulates critical thinking
  • Covers edge cases (like different languages)
  • Exposes poorly written code
  • Confidence when improving/refactoring code
  • Confidence when adding features
  • Confidence when upgrading related software
  • Confidence during code deployments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When should you not write tests?

A
  • Very small applications.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What makes testing easier?

A

By thinking in user stories

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

What do we want to test?

A
  • Happy path
  • Sad path
  • Edge cases
  • Bug fixes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What do we not want to test?

A
  • Basic ruby
  • Basic ruby on rails
  • Third party APIs
  • Behaviors that have been tested already
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between .rspec and .rspec-local ?

A

.rspec is the global configurations that are used by all the team members, .rspec-local are the local configurations on your computer. So if you want to have your own configurations that should not be shared with the other team members then you use the .rspec-local

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

What is the first thing you add in a spec file?

A

The first thing you must do is to require the file you want to test.

we want to require the file car.rb

ex
require ‘car’

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

what is describe used for?

A

describe is used to define an example group.

A good practice is to have a describe for each method

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

What is context?

A

Context is used an alias for describe these are used when you want to group the methods for a specific usecase

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

What is the fundamental syntax of the expectations?

A

expect( ).to ( )
and
expect( ).not_to ( )

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

What is the RSpec Hierarchy?

A

It is the order that we group our specs (fundamental structure)

1 Spec file = car_spec.rb
2 example group = describe
3 nested group = describe/context
4 example = it
5 expectations = expect(  ).to (  )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you describe a class method?

A

describe .method do

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

How do you describe an instance method?

A

describe #method do

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

How do we skip over describe blocks?

A

You write an x in front of the describe
xdescribe ‘something

or you write skip(‘message’) inside the describe block

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

How do you skip specs?

A

You write an x in front of it

xit ‘something’

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

How do you mark a block as pending?

A

you use the word pending inside the block.

pending(‘pending message’)

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

What is the difference between pending and skip?

A

Pending still runs the code and gives failiures while skip skips the test altoghether.

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

How many expectations should you have for an example?

A

Only one

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

How is a matcher constructed?

A

expect(actual).to match(expected)

24
Q

What are matchers?

A

Matchers are used as arguments to #to( )
Expectations are simple statements
Matchers provide the variety and complexity

25
What kinds of matchers are available?
``` Equivalence mathcers Truthiness matchers Numeric-comparison mathcers Collection matchers Obeservation matchers ```
26
What are truthiness matchers?
Truthiness matchers checks if something is true or not
27
What are numeric matcheres>
Numeric matchers match if two numeric matchers are the same
28
What are collection matchers?
Collection matchers compare collections for example arrays
29
What are predictive matchers?
Predicitve matchers are dynamic.
30
What are observation matchers?
They allow us to create expectations that on how objects change - we can observe how values change - watch for errors - watch for output the syntax for observation matchers is expect { }.to ( )
31
What are compound expectations?
They give us the ability to use And and Or in our expectations you can also use & for And and | for Or
32
What are composing matchers?
``` They allow us to use other matchers as the argument of the matcher we are working with. some of these are: - all(matcher) - include(matcher, matcher) - start_with(matcher) - end_with(matcher) - contain_exactly(matcher, matcher, matcher) - match(matcher) - change { }.from(matcher) to.(matcher) - change { }.by(matcher) - output(matcher).to_stdout - raise_error.with_message(matcher) ```
33
What are Hooks?
A hook is functionality provided by software for users of that software to have their own code called under certain circumstances. That code can augment or replace the current code. It is how you keep your specs DRY
34
What are before hooks?
before hooks run before the spec runs
35
How do you use before hooks?
before(:context) do end # before any of the it blocks are executed before(:example) do end # runs before the specific it block
36
How do you use after hooks?
after(:context) do end # after any of the it blocks are executed after(:example) do end # runs after the specific it block
37
What is the around hook?
it runs both before and after the code is executed
38
How do you make objects and values available to the hooks?
You need to use instance variables to make objects available to examples
39
What is the let method?
the let method is an alternative to the before method, it is used when we only have an instance variable that we want to make available to the specs. The let method creates a helper method for you that you can use in your specs instead of the before and after methods. let(:car) { Car.new }
40
How do you set a subject?
subject { Car.new }
41
What is a subject?
subject is the same as the let method but works as a shorthand.
42
What is implicit subject?
when you describe a class it gives the specs the ability to use subject describe Car instead of describe 'Car'
43
What are shared examples?
Shared examples gives us the ability to share specs between different classes for behaviors that are alike
44
What are test doubles?
Test doubles are objects that work as stand in for another object Just like a stunt man stands in for the real actor.
45
What are some differnt kind of test doubles?
``` Doubles Mocks Stubs Fakes Spies Dummies ```
46
Why should we use test doubles?
- the real objects are difficult or expansive to work with (like we need a internet connection or requires the database) - A simpler version will serve our purpose as well - Responses are unpredictable - Having fixed responses makes testing easier
47
What do test doubles do?
- set known return values - fake method implementations - Set expectations about calls to an object
48
What is the definition of an double/mock?
A simple object preprogrammed with expectations and responses as preperation for the calls it will recieve
49
What is the definition of a stub?
An instruction to an object to return a specific response to a method call
50
What are mocks?
Mocks are objects that are stand ins for other objects
51
What are stubs?
Stubs gives mocks the behaviour we want.
52
What are partial doubles?
Partial doubles allow us to use a part of the double.
53
How do message expectations work?
We allow something to happen but at the same time we are setting an additional requirement that it must happen or else our example is going to fail.
54
How to plan for a test?
- Ask what to test? - Happy path, sad path, edge cases? - Basic usage vs advanced usage?
55
What are fixtures?
fixtures are a fancy word for sample data - Stored as YAML files in spec/fixtures - Rspec loads the fixtures into the test database before the test suite runs.
56
How do you write a fixture?
in your fixture file bob: name: Bob Schmob city: Chicago country: USA faraz: name: Faraz Naeem city: Stockholm country: Sweden
57
What is a factory?
Factory is an alternative to fixtures and are stored in spec/factories - Factories are defined but not loaded in the database.