How do I prevent RSpec from using "Rspec.describe... " and "type: ..." in generated specs?

468 views Asked by At

Since upgrading to RSpec 3 my generated spec files contain RSpec.describe instead of just describe, and explicitly include the type with e.g. :type => :model. For example, here's the model spec file that was just generated for a class called "Plan":

require 'rails_helper'

RSpec.describe Plan, :type => :model do
  pending "add some examples to (or delete) #{__FILE__}"
end

I'd rather have it look like this (note the changes on line 3):

require 'rails_helper'

describe Plan do
  pending "add some examples to (or delete) #{__FILE__}"
end

... the "type" call seems especially redundant because I have the line config.infer_spec_type_from_file_location! in rails_helper.

How can I make RSpec generate spec files which look like my 2nd example? I don't want to have to manually edit it every time.

1

There are 1 answers

0
smallbutton On BEST ANSWER

If you look at the template in the rspec-rails github repo you can see there is no option to do this via config variables. But you should be able to customize it by just creating a file in your lib/generators/rspec/model/templates/ that is called model_spec.rb for creating a custom generator for your models. It's just an erb template as you can see in the link above:

require 'rails_helper'

<% module_namespacing do -%>
RSpec.describe <%= class_name %>, :type => :model do
  pending "add some examples to (or delete) #{__FILE__}"
end
<% end -%>

You should be easily able to customize any generator that Rspec defines with this approach. Yoe can even add extra things. Futher Info is here. Hope this helpes you :)