Using Given-When-Then in RSpec
RSpec provides a way to use Given-When-Then in test classes by using aliases. Aliases are a way to define new names for existing RSpec methods. By using aliases, developers can write tests that use Given-When-Then syntax.
RSpec is a testing tool for Ruby, created for behavior-driven development (BDD / TDD). It is the most frequently used testing library for Ruby in production applications. RSpec provides a powerful DSL (domain-specific language) that allows developers to write expressive and readable tests. Also there are a number of other popular testing libraries for Ruby, such as Test::Unit and Minitest.
but always the lazy one in me wants something easier to read and remember when coding tests
require 'spec_helper'
RSpec.testing 'Calculator' do
Given 'a calculator' do
let(:calculator) { Calculator.new }
When 'the calculator adds two numbers' do
before_that do
@result = calculator.add(2, 3)
end
Then 'the result should be the sum of the two numbers' do
expect(@result).to eq(5)
end
end
end
end
was it easy to read?
In the code example above, we define aliases for example_group and example elements using the keywords Given, When, and Then. We also define a helper method before_that
that is used as wrapper of the before(:each) method. Also we use 'testing' as an alias for 'describe'
We have capitalized the words Given When Then so that they are not confused with Ruby reserved words that are the same but lowercase.
We then define a test class for a Calculator object. We use the Given
alias to define a context for the test, and the When
alias to define the behavior being tested. We use the before_that
helper method to set up the preconditions for the test. Finally, we use the Then alias
to define the expected outcome of the test.
What is Given-When-Then?
Given-When-Then is a test description technique that helps developers to write tests that are easy to read and understand. The technique is based on three parts:
- Given: This part describes the preconditions of the test. It sets up the context for the test.
- When: This part describes the action that is being tested. It specifies the behavior that is being tested.
- Then: This part describes the expected outcome of the test. It specifies the result that is expected from the behavior being tested.
How to do use aliases on RSpec tests
To enable this aliases just add these sentences on the spec_helper.rb:
RSpec.configure do |config|
....
config.alias_example_group_to :testing
config.alias_example_group_to :Given
config.alias_example_group_to :When
config.alias_example_to :Then
def before_that(&block)
before(:each, &block)
end
end
class Calculator
def add(int_a, int_b)
int_a + int_b
end
end
IMHO Given-When-Then is a powerful test description technique that helps developers to write tests that are easy to read and understand. RSpec provides aliases that allow developers to use Given-When-Then syntax in their tests. By using aliases on RSpec, developers can write tests that are expressive and readable, making it easier to maintain and debug their code.
By following these dev tricks, you can write Given-When-Then tests that are easy to read and understand. This will make it easier to maintain, document and debug your code.
Top comments (0)