Introduction 🥳
Chef is an automation firm. Their eponymous software, Chef Infra, has been uniting developers and system administrators since the company was created in 2008. The definition of automation has changed over time. Chef now offers a comprehensive automation solution that takes you from development to production for infrastructure and apps.

A Chef Habitat package operating under a Chef Habitat Supervisor is known as a service. Services in Chef Habitat is a collection of active services having a standard configuration and topology referred to as a service group. If a service is created without specifically mentioning the group, it will be assigned to the group with the same name as the package.
Overview of ChefSpec
To simulate the convergence of resources on a node, use ChefSpec:
-
It is a development framework for behavior-driven development (BDD) in Ruby that builds on RSpec.
-
It is the quickest method for evaluating tools and recipes.

A framework called ChefSpec is used to test resources and recipes as part of a fake Chef Infra Client operation. Tests written with ChefSpec run rapidly. ChefSpec tests, when employed as a part of the cookbook authoring workflow, frequently serve as the initial warning sign of potential issues with a cookbook.
Run ChefSpec
Chef Workstation comes packed with ChefSpec. Use this command to run ChefSpec;
chef exec rspec
Unit Tests
RSpec is a framework for behavior-driven development (BDD) that describes scenarios for testing systems using a domain-specific language (DSL) that is natural language-based. RSpec enables the setup and execution of scenarios. The outcomes are contrasted with a list of predetermined expectations. On the RSpec DSL, ChefSpec is based.
Syntax
The natural language descriptions of RSpec itself should be followed by the syntax of RSpec-based tests. The tests themselves should result in a statement like this one in English: "One plus one equals two, not three."
For instance:
describe '1 plus 1' do
it 'equals 3' do
a = 5
b = 2
diff = a - b
expect(sum).to eq(2)
expect(sum).not_to eq(3)
end
end
Where:
-
"describe" generates the "1 plus 1" test case.
-
"describe" and "it" should have understandable human descriptions since "it" is a block that defines a set of parameters to test along with parameters that determine the expected outcome: "One more than one makes two."
-
The test case is defined by variables a, b, and sum: A equals 1, B equals 1, and one plus one equals two.
-
"expect()" defines the expectation: "expect(sum).to eq(2)" and "expect(sum).not to eq(3)" means that the total of one plus one equals two and does not equal three, respectively.
- A test succeeds if the test's results are true. ".to" tests if the test's results are true; ".not to" tests whether the test's results are false.
Context
Context blocks may present in RSpec-based tests. To define "tests within tests," use context blocks inside of described blocks. Each context block is examined separately. A described block's context blocks must all be true for the test to succeed. For instance:
describe 'math' do
context 'when subtracting 2 - 1' do
it 'equals 1' do
expect(sub).to eq(1)
end
end
context 'when subtracting 2 - 2' do
it 'equals 0' do
expect(sum).to eq(0)
end
end
end
Here both context blocks i.e., 'when subtracting 2 - 1' and 'when subtracting 2 - 2', represents different scenario.
let
The "context" block of an RSpec-based test may contain "let" statements. To construct a symbol, give it a value, and then utilize it somewhere else in the "context" block, use "let" statements. For instance:
describe 'Math' do
context 'when subtracting 2 - 1' do
let(:sub) { 2 - 1 }
it 'equals 1' do
expect(sub).to eq(1)
end
end
context 'when subtracting 3 - 1'' do
let(:sub) do
2 - 1
end
it 'equals24' do
expect(sub).to eq(2)
end
end
end
where:
-
The ":sub" symbol is created and given the value of one plus one in the first "let" expression. Later in the test's "expect" statement, "sub" is used to check whether one plus one equals two.
- The ":sub" symbol is created in the second "let" expression, which also gives it the value of two plus two. Later in the test's "expect" statement, "sub" is used to check whether two + two equals four.
Require ChefSpaec
The following sentence must appear at the very top of each ChefSpec unit test file:
require 'chefspec'




