picardo
picardo

Reputation: 24886

How do I generate specs for existing controllers?

I have several controllers already set up. Now I want to start writing spec tests for them. Is there a command that generates the spec files automatically? I know rails does this for new resources, but I don't know if it does it for existing controllers/models too.

Upvotes: 30

Views: 12880

Answers (3)

Alter Lagos
Alter Lagos

Reputation: 12540

There are two options. If you want an empty spec file, you could try with:

rails g rspec:controller ControllerName

Now, if you want a spec file with initial specs for a basic REST controller, try with:

rails g rspec:scaffold ControllerName

Upvotes: 10

Josiah Kiehl
Josiah Kiehl

Reputation: 3633

If you've configured rspec in application.rb:

config.generators do |g|
  g.test_framework      :rspec
end

then rails g controller things will work. Opt not to overwrite files as they're generated.

All a spec looks like when it's generated is the following:

require 'spec_helper'

describe ThingsController do

  it "should be successful" do
    get :index
    response.should be_successful
  end

end

I often create the specs manually, as it's rather trivial.

Upvotes: 4

Ryan Bigg
Ryan Bigg

Reputation: 107718

rails g rspec:controller ControllerName

When it asks you to override the existing controller, type n.

Upvotes: 44

Related Questions