Creating rspec files for an existing model (newbie)

I have a Rails application with one model (after the tutorial), and I forgot to install the Rspec gem from the start. I did this, but Spec files and folders are not displayed.

I read the following: How do I create specifications for existing controllers? and tried several commands that generated the files (I'm so noob I can't find out what he really did)

But no new Spec!

Will it be because I have a model, not a controller? What will be the correct command line? Alternatively, can I create them directly and with what name?

+6
source share
2 answers

First delete the test directory, then run the following command

 rails generate rspec:install 

And if you use bundler, use the following command instead

 bundle exec rails generate rspec:install 

It should create a spec directory in your rails root

In this directory you can find the following file among other spec_helper.rb directories

Then you can edit this file if you need

And to run the tests you have to use the following command

  rspec 

Or if you use bundler

 bundle exec rspec 
+9
source

Run the generators again:

rails generate rspec:model MyModel

rails generate rspec:controller MyController

He will ask if you want to override existing files. Answer according to the situation. You probably want to save existing model / controller files. New controller specifications should end (for example) spec/controllers/my_controller_spec.rb .

Or you can simply add specification files manually. See the rspec-rails documentation for more details.

I also recommend reading the Rails Application Testing Guide .

+29
source

Source: https://habr.com/ru/post/971944/


All Articles