Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does including the type in the describe block of a rspec do?

I don't think the type part is necessary, what does it actually do?

RSpec.describe Auction, :type => :model do
like image 905
stackjlei Avatar asked Jul 10 '17 23:07

stackjlei


People also ask

What does RSpec describe do?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

Is it possible to nest describe blocks RSpec?

You CAN nest contexts in RSpec and they will work. The issue is that it is a somewhat polarized topic, and some people will rather stay away from it. Some people will tell you it's a good practice, others will tell you it's not...

What does before do in RSpec?

The before(:each) method is where we define the setup code. When you pass the :each argument, you are instructing the before method to run before each example in your Example Group i.e. the two it blocks inside the describe block in the code above.

What is context in RSpec?

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.


1 Answers

The type metadata is necessary to include the correct rspec-rails support functions. There can be different spec types, inclucing controller, view, helper, mailer etc. Look more from here. Model specs are more specifically described here.

Note: RSpec versions before 3.0.0 automatically added metadata to specs based on their location on the filesystem. In RSpec3 this behaviour must be defined separately in configuration:

​# spec/rails_helper.rb
RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

Therefore - if you are using RSpec 3, then without the upper configuration you cannot ignore the type declaration when creating specs.

There is also possibility to define your own custom metadata type like this:

​# set `:type` for serializers directory
RSpec.configure do |config|
  config.define_derived_metadata(:file_path => Regexp.new('/spec/serializers/')) do |metadata|
    metadata[:type] = :serializer
  end
end
like image 123
Andres Avatar answered Nov 15 '22 11:11

Andres