Sha256: 66242493506a1a86cde9d7f94fc4797f59d33a7498e833e20d1bb350a7763f68

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

require 'test_helper'
require 'rails/generators/test_case'
require 'generators/rails/scaffold_controller_generator'

if Rails::VERSION::MAJOR > 4

  class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
    tests Rails::Generators::ScaffoldControllerGenerator
    arguments %w(Post title body:text --api)
    destination File.expand_path('../tmp', __FILE__)
    setup :prepare_destination

    test 'controller content' do
      run_generator

      assert_file 'app/controllers/posts_controller.rb' do |content|
        assert_instance_method :index, content do |m|
          assert_match %r{@posts = Post\.all}, m
        end

        assert_instance_method :show, content do |m|
          assert m.blank?
        end

        assert_instance_method :create, content do |m|
          assert_match %r{@post = Post\.new\(post_params\)}, m
          assert_match %r{@post\.save}, m
          assert_match %r{render :show, status: :created, location: @post}, m
          assert_match %r{render json: @post\.errors, status: :unprocessable_entity}, m
        end

        assert_instance_method :update, content do |m|
          assert_match %r{render :show, status: :ok, location: @post}, m
          assert_match %r{render json: @post.errors, status: :unprocessable_entity}, m
        end

        assert_instance_method :destroy, content do |m|
          assert_match %r{@post\.destroy}, m
        end

        assert_match %r{def post_params}, content
        assert_match %r{params\.require\(:post\)\.permit\(:title, :body\)}, content
      end
    end

    test 'dont use require and permit if there are no attributes' do
      run_generator %w(Post --api)

      assert_file 'app/controllers/posts_controller.rb' do |content|
        assert_match %r{def post_params}, content
        assert_match %r{params\.fetch\(:post, \{\}\)}, content
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jbuilder-2.8.0 test/scaffold_api_controller_generator_test.rb
jbuilder-2.7.0 test/scaffold_api_controller_generator_test.rb
jbuilder-2.6.4 test/scaffold_api_controller_generator_test.rb
jbuilder-2.6.3 test/scaffold_api_controller_generator_test.rb
jbuilder-2.6.2 test/scaffold_api_controller_generator_test.rb
jbuilder-2.6.1 test/scaffold_api_controller_generator_test.rb