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
Q

What kinds of matchers are available?

A
Equivalence mathcers
Truthiness matchers
Numeric-comparison mathcers
Collection matchers
Obeservation matchers
26
Q

What are truthiness matchers?

A

Truthiness matchers checks if something is true or not

27
Q

What are numeric matcheres>

A

Numeric matchers match if two numeric matchers are the same

28
Q

What are collection matchers?

A

Collection matchers compare collections for example arrays

29
Q

What are predictive matchers?

A

Predicitve matchers are dynamic.

30
Q

What are observation matchers?

A

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
Q

What are compound expectations?

A

They give us the ability to use And and Or in our expectations
you can also use & for And
and | for Or

32
Q

What are composing matchers?

A
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
Q

What are Hooks?

A

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
Q

What are before hooks?

A

before hooks run before the spec runs

35
Q

How do you use before hooks?

A

before(:context) do
end # before any of the it blocks are executed

before(:example) do
end # runs before the specific it block

36
Q

How do you use after hooks?

A

after(:context) do
end # after any of the it blocks are executed

after(:example) do
end # runs after the specific it block

37
Q

What is the around hook?

A

it runs both before and after the code is executed

38
Q

How do you make objects and values available to the hooks?

A

You need to use instance variables to make objects available to examples

39
Q

What is the let method?

A

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
Q

How do you set a subject?

A

subject { Car.new }

41
Q

What is a subject?

A

subject is the same as the let method but works as a shorthand.

42
Q

What is implicit subject?

A

when you describe a class it gives the specs the ability to use subject

describe Car instead of
describe ‘Car’

43
Q

What are shared examples?

A

Shared examples gives us the ability to share specs between different classes for behaviors that are alike

44
Q

What are test doubles?

A

Test doubles are objects that work as stand in for another object
Just like a stunt man stands in for the real actor.

45
Q

What are some differnt kind of test doubles?

A
Doubles
Mocks
Stubs
Fakes
Spies
Dummies
46
Q

Why should we use test doubles?

A
  • 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
Q

What do test doubles do?

A
  • set known return values
  • fake method implementations
  • Set expectations about calls to an object
48
Q

What is the definition of an double/mock?

A

A simple object preprogrammed with expectations and responses as preperation for the calls it will recieve

49
Q

What is the definition of a stub?

A

An instruction to an object to return a specific response to a method call

50
Q

What are mocks?

A

Mocks are objects that are stand ins for other objects

51
Q

What are stubs?

A

Stubs gives mocks the behaviour we want.

52
Q

What are partial doubles?

A

Partial doubles allow us to use a part of the double.

53
Q

How do message expectations work?

A

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
Q

How to plan for a test?

A
  • Ask what to test?
  • Happy path, sad path, edge cases?
  • Basic usage vs advanced usage?
55
Q

What are fixtures?

A

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
Q

How do you write a fixture?

A

in your fixture file

bob:

name: Bob Schmob
city: Chicago
country: USA

faraz:

name: Faraz Naeem
city: Stockholm
country: Sweden

57
Q

What is a factory?

A

Factory is an alternative to fixtures
and are stored in spec/factories
- Factories are defined but not loaded in the database.