Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating rspec files for existing model (beginner)

I have a Rails application with one model (following a tutorial) and I forgot to install the gem Rspec at inception. I have now done it but the Spec files and folders don't show up.

I have read the following : How do I generate specs for existing controllers? and tried a few command lines that have generated files (am so noob I cant find out what it has actually done)

But no new Spec files !

Would it be because I have a Model and not a Controller ? What would be the correct command line ? Alternatively may I create them directly and with what name?

like image 527
Maxence Avatar asked Jul 08 '14 23:07

Maxence


People also ask

How do I run an rspec on a specific file?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

How do I set up rspec?

Installing RSpec Boot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.


1 Answers

Run the generators again:

rails generate rspec:model MyModel
rails generate rspec:controller MyController

It will ask if you want to override existing files. Answer as appropriate for your situation. You probably want to keep your existing model/controller files. The new controller specs should end up in (for example) spec/controllers/my_controller_spec.rb.

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

I also recommend reading the Guide to Testing Rails Applications.

like image 98
zetetic Avatar answered Oct 06 '22 06:10

zetetic