How do I generate specs for existing controllers?

本秂侑毒 提交于 2019-12-28 11:50:09

问题


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.


回答1:


rails g rspec:controller ControllerName

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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/4235763/how-do-i-generate-specs-for-existing-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!