How do I generate specs for existing controllers?

后端 未结 3 1040
孤城傲影
孤城傲影 2020-12-08 14:11

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

相关标签:
3条回答
  • 2020-12-08 14:57

    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
    
    0 讨论(0)
  • 2020-12-08 14:58
    rails g rspec:controller ControllerName
    

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

    0 讨论(0)
  • 2020-12-08 14:59

    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.

    0 讨论(0)
提交回复
热议问题