I don't think the type
part is necessary, what does it actually do?
RSpec.describe Auction, :type => :model 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.
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...
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With