I have a basic doubt.
If the rspec file contains many contexts:
describe Name do
context "number1" do
.......
.......
end
context "number 2" do
.......
.......
end
context "number 3" do
.......
.......
end
How should the functions from each of the contexts be described in the .rb file? Should they be in the same class or different class? Is there any book I can read to improve my knowledge about this?
The structure I use when defining rspec files (based on reading I’ve done on rspec) is that you use
describesto describe specific functions, andcontextto talk about a specific context of state and/or path through the function.Example class:
As you can see, I’ve defined a class method and an instance method that do really silly and random functions. But the point is this: the class method will do something different based on the argument, and the instance method will do something different based on some outside factor: you need to test all these, and these are different contexts. But we will describe the functions in the rspec file.
Rspec file:
So you can see the
describeis for saying what method you’re describing, and matches up with the name of a method in your class. Thecontextis used to evaluate different conditions the method can be called in, or different states that affect the way the method works.Hope this helps!