What is subject in RSpec?

Summary: RSpec’s subject is a special variable that refers to the object being tested. Expectations can be set on it implicitly, which supports one-line examples. It is clear to the reader in some idiomatic cases, but is otherwise hard to understand and should be avoided.

What is stub RSpec?

In RSpec, a stub is often called a Method Stub, it’s a special type of method that “stands in” for an existing method, or for a method that doesn’t even exist yet. Here is the code from the section on RSpec Doubles − class ClassRoom def initialize(students) @students = students End def list_student_names @students.

What is do not stub method of object under test?

The goal of the guideline “Don’t Stub the System Under Test” is to help us use tests as a guide for when to split up a class. If a behavior is so complicated that we felt compelled to stub it out in a test, that behavior is its own concern that should be encapsulated in a class.

What is the difference between a mock and a stub?

Stub: a dummy piece of code that lets the test run, but you don’t care what happens to it. Mock: a dummy piece of code, that you VERIFY is called correctly as part of the test.

How do I name a subject in RSpec?

RSpec lets you declare an “implicit subject” using `subject { … }` which allows for tests like `it { should be_valid }`. If you need to reference your test subject you should explicitly name it using `subject(:your_subject_name) { … }`.

What is the difference between stubs and mocks in Ruby testing?

A Test Stub is a fake thing you stick in there to trick your program into working properly under test. A Mock Object is a fake thing you stick in there to spy on your program in the cases where you’re not able to test something directly.

What is stub code?

A method stub or simply stub in software development is a piece of code used to stand in for some other programming functionality. A stub may simulate the behavior of existing code (such as a procedure on a remote machine; such methods are often called mocks) or be a temporary substitute for yet-to-be-developed code.

What is difference between stub and driver in testing?

Stubs are used when lower-level of modules are missing or in a partially developed phase, and we want to test the main module. Drivers are used when higher-level of modules are missing or in a partially developed phase, and we want to test the lower(sub)- module.

What is the difference between context and description?

According to the rspec source code, “context” is just a alias method of “describe”, meaning that there is no functional difference between these two methods. However, there is a contextual difference that’ll help to make your tests more understandable by using both of them.